美文网首页
JS案例10-选项卡

JS案例10-选项卡

作者: hi__world | 来源:发表于2018-10-20 19:42 被阅读0次

    效果:


    bea2d637e499aea913685ef7130b6dcb.gif

    源码:

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            .box {
                width: 500px;
                height: 400px;
                border: 1px solid #ccc;
                margin: 100px auto;
                overflow: hidden;
            }
            ul {
                width: 600px;
                height: 40px;
                margin-left: -1px;
                list-style: none;
            }
            li {
                float: left;
                width: 101px;
                height: 40px;
                text-align: center;
                font: 600 18px/40px "simsun";
                background-color: pink;
                cursor: pointer;
            }
            span {
                display: none;
                width: 500px;
                height: 360px;
                background-color: green;
                text-align: center;
                font: 700 150px/360px "simsun";
            }
            .show {
                display: block;
            }
            .current {
                background-color: blue;
            }
        </style>
    
        <script>
            window.onload = function () {
                //需求:鼠标放到上面的li上,li本身变色(添加类),对应的span也显示出来(添加类);
                //思路:1.点亮盒子。   2.利用索引值显示盒子。
                //步骤:
                //1.获取事件源和相关元素
                //2.绑定事件
                //3.书写事件驱动程序(排他思想)
    
    
                //1.获取事件源和相关元素
                var liArr = document.getElementsByTagName("li");
                var spanArr = document.getElementsByTagName("span");
                //2.绑定事件(循环绑定)
                for(var i=0;i<liArr.length;i++){
                    //绑定索引值
                    liArr[i].index = i;
                    liArr[i].onmouseover = function () {
                        //3.书写事件驱动程序(排他思想)
                        //1.点亮盒子。   2.利用索引值显示盒子。(排他思想)
                        for(var j=0;j<liArr.length;j++){
                            liArr[j].className = "";
                            spanArr[j].className = "";
                        }
                        this.className = "current";
                        spanArr[this.index].className = "show";
                    }
                }
            }
        </script>
    </head>
    <body>
    
        <div class="box">
            <ul>
                <li class="current">鞋子</li>
                <li>袜子</li>
                <li>帽子</li>
                <li>裤子</li>
                <li>裙子</li>
            </ul>
            <span class="show">鞋子</span>
            <span>袜子</span>
            <span>帽子</span>
            <span>裤子</span>
            <span>裙子</span>
        </div>
    
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:JS案例10-选项卡

          本文链接:https://www.haomeiwen.com/subject/ncddzftx.html