Jquery 无法删除我添加的div
我使用以下代码向页面添加了一些元素,效果很好:Jquery 无法删除我添加的div,jquery,Jquery,我使用以下代码向页面添加了一些元素,效果很好: $("body").on("click", "#buttonadd", function(){ var vriend = "<div id='friends'><h4>Jaap Smit</h4><div class='unfollow'><a href='#' class='removefriend'>Delete friend</a></div><
$("body").on("click", "#buttonadd", function(){
var vriend = "<div id='friends'><h4>Jaap Smit</h4><div class='unfollow'><a href='#' class='removefriend'>Delete friend</a></div></div>";
$(this).parent().siblings(".currentfriends").append(vriend);
});
我已经试了很长时间了,但我不知道如何修复它。有什么想法吗?课堂上没有什么
vriend
。您可能想删除#好友
$("body").on("click", "a.removefriend", function(){
$(this).closest('#friends').remove();
});
您正试图使用
$(“.vriend”)
而不是定义的变量删除类为“vriend”的对象。因为(至少在显示的代码中)没有合适的目标,所以什么都没有发生。
请尝试$(this).closest(vriend.remove()代码>取而代之
$("body").on("click", "a.removefriend", function(){
$(this).closest('#friends').remove();
});