【JS】Tab切换效果

Osinghong4周前 (05-19)成果展示58
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>d24c04429</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        li {
            list-style-type: none;
        }
        .tab {
            width: 978px;
            margin: 10px auto;
        }
        .tab_list {
            height: 39px;
            border-bottom: 2px solid #ccc;
            background-color: #f1f1f1;
        }
        .tab_list li {
            float: left;
            height: 39px;
            line-height: 39px;
            padding: 0 20px;
            text-align: center;
            cursor: pointer;
        }
        .tab_list .current {
            background-color: #c81623;
            color: #fff;
        }
        .item_info {
            padding: 20px 0 0 20px;
        }
        .item {
            display: none;
        }
        .tab_con {
            min-height: 200px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <div class="tab">
        <ul class="tab_list">
            <li class="current">商品介绍</li>
            <li>规格与包装</li>
            <li>售后保障</li>
            <li>商品评价</li>
            <li>手机社区</li>
        </ul>
        <div class="tab_con">
            <div class="item" style="display: block;">商品介绍内容</div>
            <div class="item">规格与包装内容</div>
            <div class="item">售后保障内容</div>
            <div class="item">商品评价内容</div>
            <div class="item">手机社区内容</div>
        </div>
    </div>
    <script>
        var lis = document.querySelectorAll('.tab_list li');
        var items = document.querySelectorAll('.item');
        for (var i = 0; i < lis.length; i++) {
            lis[i].index = i;
            lis[i].onclick = function() {
                for (var j = 0; j < lis.length; j++) {
                    lis[j].className = '';
                    items[j].style.display = 'none';
                }
                this.className = 'current';
                items[this.index].style.display = 'block';
            }
        }
    </script>
</body>
</html>


打赏
分享给朋友:

相关文章

【H5】表单7个月前 (11-27)
【H5】ul,li练习6个月前 (12-04)
【JS】三元表达式3个月前 (03-17)
【JS】While循环3个月前 (03-24)