Javascript 使用jQuery以特定类为目标
我试图在jQuery中显示一个隐藏的类,但它不适用于我所针对的类。它显示整个类:Javascript 使用jQuery以特定类为目标,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我试图在jQuery中显示一个隐藏的类,但它不适用于我所针对的类。它显示整个类: <div class="feeds"> <input class="post_id" /> <textarea class="commentdata"></textarea> <button class="mmore">More</button> <p class="time">time</p> &l
<div class="feeds">
<input class="post_id" />
<textarea class="commentdata"></textarea>
<button class="mmore">More</button>
<p class="time">time</p>
<div class = "comment_data">
<div class = "comment">
<p> Comments </p>
</div>
</div>
</div>
<div class="feeds">
<input class="post_id" />
<textarea class="commentdata"></textarea>
<button class="mmore">More</button>
<p class="time">time</p>
<div class = "comment_data">
<div class = "comment">
<p> Comments </p>
</div>
</div>
您正在选择与选择器
.comment\u data
匹配的所有对象
您需要将其限制在单击按钮所在的容器中。有很多方法可以做到这一点。一个简单的方法是:
$( ".mmore" ).click(function() {
$(this).parent().find('.comment_data').slideToggle("slow");
});
您可以使用.closest()
查找.feeds div,然后使用.find()
查找*.comment\u data*div
$('.mmore').click(function() {
$(this).closest('.feeds').find('.comment_data').slideToggle('slow')
});
这更方便,因为它更少地依赖于html结构。你刚刚有效地结束了我的一天。非常感谢。
$('.mmore').click(function() {
$(this).parents('.feeds').find('.comment_data').slideToggle('slow')
});
$( ".mmore" ).click(function() {
$(this).parent().find('.comment_data').slideToggle("slow");
});
$('.mmore').click(function() {
$(this).closest('.feeds').find('.comment_data').slideToggle('slow')
});