【JS】标签页切换

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>选项卡</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul, ol, li {
            list-style: none;
        }

        .box {
            width: 600px;
            height: 400px;
            border: 3px solid pink;
            margin: 50px auto;
            display: flex;
            flex-direction: column;
        }

        .box > ul {
            height: 60px;
            display: flex;
        }

        .box > ul > li {
            flex: 1;
            color: #fff;
            background-color: skyblue;
            font-size: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
        }

        .box > ul > li.active {
            background-color: #fde348;
        }

        .box > ol {
            flex: 1;
            position: relative;
        }

        .box > ol > li {
            width: 100%;
            height: 100%;
            background-color: #fc4a62;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: 100px;
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }

        .box > ol > li.active {
            display: flex;
        }

    </style>
</head>
<body>
<div class="box">
    <ul>
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <ol>
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
    </ol>
</div>
<script>
    var btns=document.querySelectorAll('ul>li');
    var tabs=document.querySelectorAll('ol>li');
//    给btns里所有按钮添加点击事件
    btns.forEach(function (item,index) {
        item.onclick=function () {
            btns.forEach(function (t,i) {
                t.className=''
                tabs[i].className=''
            })
            item.className='active'
            tabs[index].className='active'
        }
    })
</script>

</body>
</html>

 

posted @   木子欢儿  阅读(239)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
主题色彩
点击右上角即可分享
微信分享提示