Tab切换

作者: Lamport | 来源:发表于2019-06-25 19:34 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title> Tab切换-JavaScript </title>
            <style type="text/css">
                *{
                    margin: 0;
                    padding: 0;
                }
                .box{
                    width: 500px;
                    height: 500px;
                    margin: 50px auto 0;
                    box-shadow: 0 0 4px #DDD;
                    position: relative;
                }
                ul:after{
                    content: "";
                    height: 0;
                    visibility: hidden;
                    clear: both;
                    display: block;
                }
                ul li{
                    list-style: none;
                    float: left;
                    width: 100px;
                    height: 50px;
                    font-size: 25px;
                    text-align: center;
                    line-height: 50px;
                    background: greenyellow;
                }
                ul li.active{
                    background: blue;
                }
                .box div{
                    width: 500px;
                    height: 450px;
                    background: red;
                    font-size: 200px;
                    text-align: center;
                    line-height: 450px;
                    position: absolute;
                    bottom: 0;
                    left: 0;
                }
                .box div.active{
                    z-index: 2;
                }
            </style>
        </head>
        <body>
            <div class="box">
                <ul id="list">
                    <li class="active">01</li>
                    <li>02</li>
                    <li>03</li>
                    <li>04</li>
                    <li>05</li>
                </ul>
                <div class="active">01</div>
                <div>02</div>
                <div>03</div>
                <div>04</div>
                <div>05</div>
            </div>
            <script type="text/javascript">
                var oBox = document.getElementsByTagName("div")[0];
                var oList = document.getElementById("list");
                var oLi = oList.getElementsByTagName("li");
                var oDiv = oBox.getElementsByTagName("div");
                
                for( var i=0;i<oLi.length;i++ ){
                    oLi[i].index = i;
                    oLi[i].onclick = function(){
                        for( var j=0;j<oDiv.length;j++ ){
                            oLi[j].className = '';
                            oDiv[j].className = '';
                        };
                        this.className = 'active';
                        oDiv[this.index].className = 'active';
                    };
                };
            </script>
        </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:Tab切换

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