【选项卡案例】(html,css,javascript)

<style>
*{
    margin: 0;
    padding: 0;
}
ul{
    list-style: none;
}
.header{
    display: flex;
    width: 500px;
}
.header li{
    flex:1;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border: 1px solid black;
}
.box{
    position: relative;
}
.box li{
    position: absolute;
    left: 0;
    top: 0;
    width: 500px;
    height: 200px;
    background-color: yellow;
    display: none;
    font-size: 50px;
    line-height: 200px;
    text-align: center;
}
.header .active{
    background-color: red;
}
.box .active{
    display: block;
}
</style>
<body>
    <ul class="header">
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <!-- 可以多增加li ,可扩展性强-->
    </ul>
    <ul class="box">
        <li class="active">111</li>
        <li>222</li>
        <li>333</li>
        <li>444</li>
        <!-- 可以多增加li ,可扩展性强-->
    </ul>
    <script>
        var oHeaderItems=document.querySelectorAll(".header li")
        var oBoxItems=document.querySelectorAll(".box li")
        for(var i=0;i<oHeaderItems.length;i++){
            //自定义属性
            oHeaderItems[i].dataset.index=i
            oHeaderItems[i].onclick=handler
        }
            function handler(){
                var index=this.dataset.index
            for(var m=0;m<oHeaderItems.length;m++){
                oHeaderItems[m].classList.remove("active")
                oBoxItems[m].classList.remove("active")
            }
            oHeaderItems[index].classList.add("active")
            oBoxItems[index].classList.add("active")
            
            }
       
    </script>
</body>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王猫粮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值