我有多个具有相同类的元素。当我在下面使用此方法时,它不起作用。
$(".tab-content")[index].addClass("active-content");
Run Code Online (Sandbox Code Playgroud)
我的代码:
HTML:
<div class="container">
<div class="content">
<ul class="tag">
<li>
<a href="javascript:;">
Lodon
</a>
</li>
<li>
<a href="javascript:;">
Paris
</a>
</li>
<li>
<a href="javascript:;">
Tokyo
</a>
</li>
</ul>
<div class="tab-content">
<h3>Lodon</h3>
<p>London is the capital of England</p>
</div>
<div class="tab-content">
<h3>Paris</h3>
<p>Paris is the capital of France</p>
</div>
<div class="tab-content">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan</p>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
和JS
$(".tag li").click(function () {
var index = $(this).index();
$(".tab-content")[index].addClass("active-content");
});
Run Code Online (Sandbox Code Playgroud)