9.jQuery京东teb栏操作案例

122 阅读1分钟

jQuery京东teb栏操作案例,我们会使用隐式迭代原理,来操作按钮和下面内容的一致性,比原生JS通过for循环以及绑定关联变量来定位要简单很多。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>隐式迭代</title>
    <script src="jquery.min.js"></script>
    <!-- 样式 -->
    <style>
     * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style-type: none;
        }
        
        .tab {
            width: 978px;
            margin: 100px auto;
        }
        
        .tab_list {
            height: 39px;
            border: 1px 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;
        }


    </style>
</head>
<body>
  <div class="tab">
    <div class="tab_list">
        <ul>
            <li class="current">商品介绍</li>
            <li>规格与包装</li>
            <li>售后保障</li>
            <li>商品评价(50000)</li>
            <li>手机社区</li>
        </ul>
    </div>
    <div class="tab_con">
        <div class="item" style="display: block;">
            商品介绍模块内容
        </div>
        <div class="item">
            规格与包装模块内容
        </div>
        <div class="item">
            售后保障模块内容
        </div>
        <div class="item">
            商品评价(50000)模块内容
        </div>
        <div class="item">
            手机社区模块内容
        </div>

    </div>
</div>


   <script>
    //  js 操作文件
    $(function(){
      // 1.原理 .点击上部的li,当前li 添加current类,其余兄弟移除类 
      $('.tab_list li').click(function(){
        //采用连式编程思路   点击的加上 其余的删除
        $(this).addClass('current').siblings().removeClass('current') ;
        // 定义一个变量获得索引号 得到当前绑定li的索引号
        var index = $(this).index() ;
        // 3.让厦门的模块和上面的索引号一致
        $('.tab_con .item').eq(index).show().siblings().hide();  // 当前索引号显示 其他的隐藏

      })

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