Javascript Jquery单击函数上没有HTML内容

Javascript Jquery单击函数上没有HTML内容,javascript,jquery,html,Javascript,Jquery,Html,在我的代码中,我有以下内容: $(document).ready(function(){ $("input#Addmore").click(function(){ $('div#add_div').append("<a class='remove' href='#'>X</a>"); }); }); $("a.remove").click(function(){ $(this).close

在我的代码中,我有以下内容:

$(document).ready(function(){           
    $("input#Addmore").click(function(){
        $('div#add_div').append("<a class='remove' href='#'>X</a>");
    });         
});
$("a.remove").click(function(){ 
    $(this).closest("div").remove();
});

但是代码不起作用,因为jQuery没有获得附加锚代码。有人能告诉我解决方案吗?

您需要将事件委托给最近的静态元素。试试这个:

$("a.remove").click(function(){ 
    $(this).closest("div").remove();
});
$('#add_div').on('click', 'a.remove', function() {
    $(this).closest("div").remove();
});
或者,如果您使用的是较旧版本的jQuery(小于1.7),请使用
delegate()
,如下所示:

$("a.remove").click(function(){ 
    $(this).closest("div").remove();
});
$('#add_div').delegate('a.remove', 'click', function() {
    $(this).closest("div").remove();
});
或者您可以尝试以下方法:

$("a.remove").click(function(){ 
    $(this).closest("div").remove();
});
  $("input#Addmore").click(function(){
       $("div#add_div").append("<a class='remove' href='javascipt:;' onclick='remove(this);return false' href='#'>X<br/></a>");
});    
请参见此处:

试试这个

$("a.remove").click(function(){ 
    $(this).closest("div").remove();
});
$('#add_div').delegate('a.remove', 'click', function() {
    $(this).closest("div").remove();
});

我必须说,在我们的社区里有一些很棒的程序员,而你就是其中之一:)……它很有效……非常有用。。