我試圖顯示和隱藏網頁的某些部分,而不顯示或隱藏其他具有類似名稱的元素,例如我想能夠顯示某些評論回覆而不會顯示具有相同類名的其他評論回覆。但我似乎不能讓我的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