JavaScript基础 tab栏切换效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        .box {
            width: 990px;
            height: 500px;
        }
        .h_box {
            width: 990px;
            height: 50px;
            background-color: #eee;
            border-bottom: 1px solid #e4393c;
        }
        .h_box ul {
            padding: 0;
        }
        .h_box ul li {
            float: left;
            list-style: none;
            text-align: center;
            line-height: 50px;
            margin-right: 20px;
            padding-left: 15px;
            padding-right: 15px;
        }
        .bg {
            background-color: #e4393c;
            color: #ffffff;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="h_box">
            <ul>
                <li class="bg">商品介绍</li>
                <li>规格与包装</li>
                <li>售后保障</li>
                <li>商品评价</li>
                <li>手机社区</li>
            </ul>
        </div>
        <div class="tab_box">
            <div class="item">商品介绍</div>
            <div class="item" style="display: none;">规格与包装</div>
            <div class="item" style="display: none;">售后保障</div>
            <div class="item" style="display: none;">商品评价</div>
            <div class="item" style="display: none;">手机社区</div>
        </div>
    </div>
    <script>
        var h_box = document.querySelector(".h_box");
        var li = h_box.querySelectorAll("li");
        var item = document.querySelectorAll(".item");
        for (var i=0;i<li.length;i++){
            // 自定义一个属性,并赋值;可以利用属性值显示对应的div
            li[i].setAttribute('data-index',i);
            // 排他思想
            li[i].onclick = function(){
                for (var i=0;i<li.length;i++){
                    li[i].className='';
                }
                this.className = 'bg';
                // 获取自定义属性的元素
                var index = this.getAttribute('data-index');
                for(var i=0;i<item.length;i++){
                    item[i].style.display = 'none';
                }
                item[index].style.display = 'block';

            }
        }
    </script>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值