我试图显示和隐藏网页的某些部分,而不显示或隐藏其他具有类似名称的元素,例如我想能够显示某些评论回复而不会显示具有相同类名的其他评论回复。但我似乎不能让我的jquery工作正确。换句话说,我想单击类view-comments
中的一个元素,并显示类名最接近的部分more-comments
显示一个隐藏和显示部分一次使用jquery
这是我简化的HTML代码。
HTML
<article>
<div>
<div>
....
<a class="view-comments" title="" href="">View Comments</a>
</div>
</div>
<section class="more-comments">
.......
</section>
</article>
<article>
<div>
<div>
....
<a class="view-comments" title="" href="">View Comments</a>
</div>
</div>
<section class="more-comments">
.......
</section>
</article>
<article>
<div>
<div>
....
<a class="view-comments" title="" href="">View Comments</a>
</div>
</div>
<section class="more-comments">
.......
</section>
</article>
<article>
<div>
<div>
....
<a class="view-comments" title="" href="">View Comments</a>
</div>
</div>
<section class="more-comments">
.......
</section>
</article>
jQuery的
$(document).ready(function() {
$('.view-comments').click(function() {
$('.more-comments').toggle($(this).hasClass('view-comments'));
$(this).toggleClass('view-comments hide-comments');
});
});
尝试http://jsfiddle.net/a8zbuj80/1这是你想要的 – guradio