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>
<!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>
<!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="jquery-2.1.4.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>
$(function(){
$(".tone li").click(function () {
$(this).siblings().removeClass("active");
$(this).addClass("active");
var text=$(this).text();
$(".box").each(function(){
if($(this).text()==text){
$(this).addClass("on");
}else{
$(this).removeClass("on");
}
})
})
});
</script>
</body>
</html>