Jquery一个链式调用实现经典选项卡

图片

在这里插入图片描述

代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }

      .box {
        width: 500px;
        margin: 100px auto;
      }

      ul {
        list-style: none;
        display: flex;
        position: relative;
        top: 1px;
      }

      li {
        width: 150px;
        margin-right: 10px;
        height: 40px;
        line-height: 40px;
        font-size: 30px;
        text-align: center;
        border: 1px solid orangered;
      }

      .box div {
        width: 500px;
        border: 1px solid orangered;
        height: 300px;
        line-height: 300px;
        text-align: center;
        font-size: 50px;
        display: none;
      }

      ul li.active {
        border-bottom: 1px solid white;
      }

      .box div.active {
        display: block;
      }
    </style>
  </head>

  <body>
    <div id="box" class="box">
      <ul id="navBox" class="navBox">
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
      </ul>
      <div class="active">一</div>
      <div>二</div>
      <div>三</div>
    </div>
    <script src="../../js/jquery-1.11.3.js"></script>
    <script>
      let $navList = $(".box li");
      let $tabList = $(".box div");
      // // 他会自动给每一个li加上点击事件,内部就给你循环了
      $navList.click(function () {
        // index()代表了当前点击元素的位置的索引
        //  eq()通过索引找到对应的元素
        
        // 1
        // let index = $(this).index(); // 获取当前元素对应的索引
        // $(this).addClass('active').siblings().removeClass('active');
        // $tabList.eq(index).addClass('active').siblings().removeClass('active');
        
        // 2.Jquery一个链式调用实现经典选项卡
        $(this)
          .addClass("active")
          .siblings()
          .removeClass("active")
          .parent()
          .nextAll()
          .eq($(this).index())
          .addClass("active")
          .siblings()
          .removeClass("active");
      });
    </script>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值