当用户单击链接时,我需要隐藏 div。
html 是这样的:
<div class="item">
<div class="entry">
Lorem ipsum
</div>
<a href="#" class="commentsToggle">Show/hide comments</a>
<div class="comments hidden">
This comments div I want show/hide
</div>
</div>
<div class="item">
<div class="entry">
Lorem ipsum
</div>
<a href="#" class="commentsToggle">Show/hide comments</a>
<div class="comments hidden">
This comments div I want show/hide
</div>
</div>
....there's multiple items after this
默认情况下,注释是隐藏的(因此类隐藏在那里)。现在,如果用户单击“显示/隐藏评论”链接,它应该显示或隐藏该特定项目的评论,具体取决于它们是否可见。现在的问题是我是否需要一些 id 来控制 js 只挂钩到那个特定的项目 ocmments,我可以用 id 来做到这一点吗?
js 类似于 atm:
$('.item .commentsToggle').click(function(){
elem = $(this).parents('.item');
$(elem).find('.comments').each(function() {
$(this).toggleClass('hidden');
});
});
如果只有一个项目可以工作,但如果有多个项目就会崩溃:/
这很简单..我只是不知道实际上该怎么做:D
http://jsfiddle.net/uB9Fb/ http://jsfiddle.net/uB9Fb/
试试这个JS:
$('.commentsToggle').click(function(){
$(this).siblings(".comments").toggleClass('hidden');
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)