采纳追加赏 2022-07-16 14:35 采纳率: 35.3%
浏览 52
已结题

Js中siblings去掉类名没有效果

tab切换,.down .box的时候siblings 去掉同类类名没有效果

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title> 效果测试 </title>
    <script src="https://code.jquery.com/jquery-1.7.2.js"></script>
</head>
<body>

<style type="text/css">
.tone{ }
.tone li{display: inline-block;padding:10px;margin: 0 10px;background:#ddd;}
.tone li.active{background:#f60}
.down{ }
.down .box{padding:10px;margin-top: 10px;background:#f8f8f8;}
.down .box.on{background: #f60;}
</style>

<ul class="tone">
  <li class="active">11</li>
  <li>22</li>
</ul>
<div class="down">
   <div class="box on">11</div>
   <div class="box">22</div>
</div>    

<script>
 $(".tone li").click(function () {
    $(this).addClass("active").siblings().removeClass('active');
    $(this).next(".box").addClass("on").siblings().removeClass('on');
})
</script>  

</body>
</html>


  • 写回答

2条回答 默认 最新

  • Heerey525 前端领域新星创作者 2022-07-16 18:36
    关注
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>效果测试</title>
        <script src="https://code.jquery.com/jquery-1.7.2.js"></script>
      </head>
      <body>
        <style type="text/css">
          .tone {
          }
          .tone li {
            display: inline-block;
            padding: 10px;
            margin: 0 10px;
            background: #ddd;
          }
          .tone li.active {
            background: #f60;
          }
          .down {
          }
          .down .box {
            padding: 10px;
            margin-top: 10px;
            background: #f8f8f8;
          }
          .down .box.on {
            background: #f60;
          }
        </style>
    
        <ul id="toneId1" class="tone">
          <li class="active" data-index="1">11</li>
          <li data-index="2">22</li>
        </ul>
        <div id="downId1" class="down">
          <div class="box on" data-index="1">11</div>
          <div class="box" data-index="2">22</div>
        </div>
    
        <ul id="toneId2" class="tone">
          <li class="active" data-index="1">11</li>
          <li data-index="2">22</li>
        </ul>
        <div id="downId2" class="down">
          <div class="box on" data-index="1">11</div>
          <div class="box" data-index="2">22</div>
        </div>
    
        <script>
          $("#toneId1 li").click(function () {
            const index = $(this)[0].dataset.index;
            $(this).addClass("active").siblings().removeClass("active");
            $(`#downId1 .box:nth-child(${index})`)
              .addClass("on")
              .siblings()
              .removeClass("on");
          });
    
          $("#toneId2 li").click(function () {
            const index = $(this)[0].dataset.index;
            $(this).addClass("active").siblings().removeClass("active");
            $(`#downId2 .box:nth-child(${index})`)
              .addClass("on")
              .siblings()
              .removeClass("on");
          });
        </script>
      </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月26日
  • 已采纳回答 7月18日
  • 修改了问题 7月16日
  • 赞助了问题酬金5元 7月16日
  • 展开全部

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备