BOM,DOM相关案例

1.模态框案例:

需求:

 打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <title></title>
        <style type="text/css">
            *{
                padding: 0;
                margin: 0;
            }
            html,body{
                height: 100%;
            }
            #box{
                width: 100%;
                height: 100%;
                background: rgba(0,0,0,.3);
            }
            #content{
                position: relative;
                top: 150px;
                width: 400px;
                height: 200px;
                line-height: 200px;
                text-align: center;
                color: red;
                background-color: #fff;
                margin: auto;
            }
            #span1{
                position: absolute;
                background-color: red;
                top: 0;
                right: 0;
                width: 30px;
                height: 30px;
                line-height: 30px;
                text-align: center;
                color: #fff;

            }
        </style>
    </head>
    <body>
        <button id="btn">弹出</button>
    </body>
    <script type="text/javascript">
        //获取dom元素 1.获取事件源
        var oBtn = document.getElementById('btn');
        //创建弹出模态框的相关DOM对象
        var oDiv = document.createElement('div');
        var oP = document.createElement('p');
        var oSpan = document.createElement('span');


        // 设置属性
        oDiv.id = 'box';
        oP.id = 'content'
        oP.innerHTML = '模态框成功弹出'
        oSpan.innerHTML = 'X';
        oSpan.id = 'span1'

        // 追加元素
        oDiv.appendChild(oP);
        oP.appendChild(oSpan);

        // 点击弹出按钮 弹出模态框
        oBtn.onclick = function(){
            //动态的添加到body中一个div
            this.parentNode.insertBefore(oDiv,oBtn)

        }
        // 点击X 关闭模态框
        oSpan.onclick = function(){
            // 移除oDiv元素
            oDiv.parentNode.removeChild(oDiv)
        }

    </script>
</html>

2.计时器:

 

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>计时器</title>
  <script>
    var intervalId;

    function f() {
      var timeStr = (new Date()).toLocaleString();
      var inputEle = document.getElementById("i1");
      inputEle.value = timeStr;
    }

    function start() {
      f();
      if (intervalId === undefined) {
        intervalId = setInterval(f, 1000);
      }
    }
    function end() {
      clearInterval(intervalId);
      intervalId = undefined;
    }

  </script>
</head>
<body>

<input type="text" id="i1">
<input type="button" value="开始" id="start" onclick="start();">
<input type="button" value="结束" id="end" onclick="end();">
</body>
</html>

 

3.点击跳转到另一个页面:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <title>注册</title>
</head>
<body>
<div>点我一下</div>
<script>
    var div = document.getElementsByTagName("div")[0];
    div.onclick = function () {
        location.href = "http://www.baidu.com";
    }

</script>
</body>
</html>

4.搜索框:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <title>搜索框示例</title>

</head>
<body>
    <input id="d1" type="text" value="请输入关键字" onblur="blur()" onfocus="focus()">

<script>
function focus(){
    var inputEle=document.getElementById("d1");
    if (inputEle.value==="请输入关键字"){
        inputEle.value="";
    }
}

function blur(){
    var inputEle=document.getElementById("d1");
    var val=inputEle.value;
    if(!val.trim()){
        inputEle.value="请输入关键字";
    }
}
</script>
</body>
</html>

 5.select联动:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <title>Title</title>
</head>
<body>

<label for="s1">省</label>
<select id="s1">
    <option value="">-请选择省-</option>
</select>

<label for="s2">市</label>
<select id="s2">

</select>

<script>
    var data = {
        "北京": ["昌平区", "海淀区", "朝阳区"],
        "上海": ["浦东区", "徐汇区", "浦东新区"],
        "山东": ["烟台", "青岛", "威海"]
    };
    var s1Ele = document.getElementById("s1");
    var s2Ele = document.getElementById("s2");
    // 1. 拿到所有的省,在s1中生成对应的option选项
    for (var i in data){
        console.log(i);
        // 1.1 创建option标签
        var tmp = document.createElement("option");
        // 1.2 把省信息添加到option标签中
        tmp.innerText = i;
        // 1.3 把创建好的option标签插入到第一个select标签中
        s1Ele.appendChild(tmp);
    }

    // 当用户选中某个省之后才做的事儿
    // 当第一个select框的值发生变化的时候触发的动作
    s1Ele.onchange = function (ev) {
        // 0. 先清空第二个select框的option
        s2Ele.innerHTML = "";
        // 1. 获取用户选中的省
        console.log(this.value);
        var p = this.value;
        // 2. 根据用户选择的省,去data中找省对应的城市数据
        var cityArray = data[p];
        // 3. 遍历所有的城市数据,给第二个select框添加option选项
        for (var j in cityArray){
            console.log(cityArray[j]);
            // 3.1 生成空的option
            var tmp = document.createElement("option");
            // 3.2 给option添加文本
            tmp.innerText = cityArray[j];
            // 3.3 把生成的option追加到第二个select标签中
            s2Ele.appendChild(tmp);
        }
    }

</script>
</body>
</html>

6.tab栏选项卡:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <title></title>
        <style type="text/css">
            *{
                padding: 0;
                margin: 0;
            }
            ul{
                list-style: none;
            }
            #tab{
                width: 480px;
                margin: 20px auto;
                border: 1px solid red;
            }
            ul{
                width: 100%;
                overflow: hidden;
            }
            ul li{
                float: left;
                width: 160px;
                height: 60px;
                line-height: 60px;
                text-align: center;
                background-color: #cccccc;
            }

            ul li a{
                text-decoration: none;
                color:black;
            }
            li.active{
                background-color: red;
            }
            p{
                display: none;
                height: 200px;
                text-align: center;
                line-height: 200px;
                background-color: red;
            }
            p.active{
                display: block;

            }

        </style>
    </head>
    <body>
        <div id="tab">
            <ul>
                <li class="active">
                    <a href="#">首页</a>
                </li>
                <li>
                    <a href="#">新闻</a>
                </li>
                <li>
                    <a href="#">图片</a>
                </li>
            </ul>
            <p class="active">首页内容</p>
            <p>新闻内容</p>
            <p>图片内容</p>


        </div>
    </body>
    <script type="text/javascript">
        window.onload = function(){
            // //需求:鼠标放到上面的li上,li本身变色(添加类),对应的p也显示出来(添加类);
                    //思路:1.点亮上面的盒子。   2.利用索引值显示下面的盒子。

            var tabli = document.getElementsByTagName('li');
            var tabContent = document.getElementsByTagName('p')

            for(var i = 0; i < tabli.length; i++){
                // 绑定索引值(新增一个自定义属性:index属性)
                tabli[i].index  = i;
                tabli[i].onclick = function(){

                    // 1.点亮上面的盒子。   2.利用索引值显示下面的盒子。(排他思想)
                    for(var j = 0; j < tabli.length; j++){
                        tabli[j].className = '';
                        tabContent[j].className = '';
                    }
                    this.className = 'active'

                    tabContent[this.index].className = 'active';//【重要代码】
                }
        }
        }

    </script>
</html>

  

 

posted @ 2018-09-06 21:47  赵振宇  阅读(150)  评论(0编辑  收藏  举报