1 回答
TA贡献1818条经验 获得超7个赞
当您获得rel价值时,您可以使用它作为选择器将活动类添加到所需的 div 中,即:$(".tab-navigation li[rel="+activeTab+"]")。
演示代码:
$(".tab_content").hide();
$(".tab_content:first").show();
$("ul.tab-navigation li").click(function() {
$(".tab_content").hide();
var activeTab = $(this).attr("rel");
$("#" + activeTab).fadeIn();
$("ul.tab-navigation li").removeClass("active-tab");
//add class where rel matches
$(".tab-navigation li[rel=" + activeTab + "]").addClass("active-tab");
});
.active-tab {
color: red
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="tab-navigation">
<li class="tab active-tab" rel="tab1">First tab</li>
<li class="tab" rel="tab2">Second tab</li>
<li class="tab" rel="tab3">Third tab</li>
</ul>
<div>
<div class="tab_container">
<div id="tab1" class="tab_content">Some Content1</div>
<div id="tab2" class="tab_content">Some Content2</div>
<div id="tab3" class="tab_content">Some Content3</div>
</div>
</div>
<ul class="tab-navigation">
<li class="tab active-tab" rel="tab1">First tab</li>
<li class="tab" rel="tab2">Second tab</li>
<li class="tab" rel="tab3">Third tab</li>
</ul>
添加回答
举报