我正在尝试为 WordPress 构建设置一个简单的评论切换。
CSS
<div class="commenttoggle">
<p class="popcom">Show Comments</p>
~~~Clickable Button~~~~~~
</div>
<div id="comments" class="comments-area">
~~~~PHP code calling WP comments~~~
</div>
jQuery
jQuery(document).ready(function($) {
$('.comments-area').hide();
$('.commenttoggle').click(function() {
$('.comments-area').toggle();
});
});
上面的代码有效,但是当我单击一篇文章下的显示评论按钮时,所有评论部分都会显示。我一直在 stackoverflow 上查看 jQuery api & 这里,但似乎找不到任何关于仅切换最接近单击事件的元素的建议。
我已经尝试过 .closest 和 .parent 但我似乎无法使其工作。我真的很感激一些代码,但也很感谢一个解释,因为我刚刚开始使用 jQuery。
这应该做你想做的:
jQuery(document).ready(function($) {
$('.comments-area').hide();
$('.commenttoggle p').click(function() {
$(this).closest("div").next('.comments-area').toggle();
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)