JavaScriptr——Tab栏切换案列

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul,
        li {
            list-style: none;
        }

        .box {
            width: 300px;
            height: 300px;
            margin: 100px auto;
            border: 1px solid#ccc;
        }

        .hd {
            width: 100%;
            height: 50px;
        }

        .hd ul {
            display: flex;
            height: 50px;
        }

        .hd li {
            width: 100%;
            line-height: 50px;
            text-align: center;
            border-right: 1px solid#fff;
            background-color: #ccc;
            cursor: pointer;
        }

        .hd li:last-child {
            border-right: none;
        }

        .hd .cls {
            background-color: rgba(126, 177, 245, .5);
        }

        .bd {
            width: 100%;
            height: 250px;
            background-color: rgba(126, 177, 245, .5);
        }

        .bd li {
            display: none;
        }

        .bd .current {
            display: block;
        }
    </style>
</head>

<body>
    <div class="box" id="box">
        <div class="hd">
            <ul>
                <li class="cls">王者荣耀</li>
                <li>QQ飞车</li>
                <li>英雄联盟</li>
                <li>刺激战场</li>
            </ul>
        </div>
        <div class="bd">
            <ul>
                <li class="current">我是王者</li>
                <li>我是飞车</li>
                <li>我是联盟</li>
                <li>我是吃鸡</li>
            </ul>
        </div>
    </div>
    <script>
        function my$(id) {
            return document.getElementById(id);
        }
        //获取最外层id
        var boxobj = my$("box");
        //获取里层第一个div
        var hdobj = boxobj.getElementsByClassName("hd")[0];
        //获取里层第二个div
        var bdobj = boxobj.getElementsByClassName("bd")[0];
        //获取里层第一个div中所有的li
        var list = hdobj.getElementsByTagName("li");
        //获取里层第二个div中所有的li
        var lists = bdobj.getElementsByTagName("li");
        //循环遍历第一个div中所有的li
        for (var i = 0; i < list.length; i++) {
            //设置第一个div中所有的li对应的索引值
            list[i].setAttribute("index", i)
            //第一个div中所有的li注册点击事件
            list[i].onclick = function () {
                //循环遍历第一个div中所有的li
                for (var j = 0; j < list.length; j++) {
                    //先删除第一个div中所有的li的类属性
                    list[j].removeAttribute("class");
                }
                //当前点击li的类属性为cls
                this.className = "cls";
                //获取当前点击的li的索引值
                var num = this.getAttribute("index");
                //循环遍历第二个div中所有的li
                for (var k = 0; k < lists.length; k++) {
                    //先删除第二个div中所有的li的类属性
                    lists[k].removeAttribute("class");
                }
                //把获取当前点击的li的索引值放入第二个div中当前的li中,并设置第二个div中当前的li的类属性
                lists[num].className = "current";
            }
        }
    </script>
</body>

</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值