当我单击以显示它们时,我需要隐藏“显示回复”。问题是,当我点击“显示回复”时,所有的“显示回复”按钮都隐藏了。我只需要隐藏我点击的那个。这是jQuery代码:
$(".replies_show").click (function(e){
$(".replies_show").replaceWith(" ");
$(this).next(".replies").show();
e.preventDefault();
});发布于 2016-07-30 05:11:14
$(".replies_show")将选择所有包含该类的元素,因此您将选择所有元素,然后再次对所有元素应用replaceWith。然而,该回调函数中的this引用了刚刚被单击的元素(即唯一被单击的元素,而不是所有被单击的元素)。
此外,不要只是为了隐藏元素而使用replaceWith函数,而要使用.hide()。
因此,请替换
$(".replies_show").replaceWith(" ");使用
$(this).hide();发布于 2016-07-30 05:11:55
您可以使用this来获取当前元素。否则,您将使用.replies_show类选择所有元素。
$('.replies_show').on('click', function(e, el) {
$(el).replaceWith(' '); // Does what you're looking for
$(el).hide(); // Might be better, depending on what you're doing
$(this).next('.replies').show();
e.preventDefault();
});
发布于 2016-07-30 05:29:05
使用.hide()函数而不是.replaceWith()
$(".replies_show").click (function(e){
$(this).hide();
$(this).next(".replies").show();
e.preventDefault();
});https://stackoverflow.com/questions/38667528
复制相似问题