jQuery事件处理程序不处理附加元素

我正在发送ajax请求,我已将结果附加到div上。 在此追加之后,如果我想点击附加的链接(exec jquery点击function),为什么点击functiondoesen’t工作? (抱歉英文不好:P)

编辑:

jQuery('.more').live("click",function() { var ID = jQuery(this).attr("id"); if(ID) { jQuery("#more"+ID).html(''); jQuery.ajax({ type: "POST", url: "loadmore.php", data: "lastid="+ ID, cache: false, success: function(html){ $("#contentWall").append(html); jQuery("#more"+ID).remove(); // removing old more button } }); } else { jQuery(".morebox").html('The End');// no results } return false; }); 

您需要使用.live().on() ,具体取决于您的jQuery版本。

常规.click()仅适用于当前在DOM中的元素,而不适用于将来的添加。

没有更多的信息很难确定,但可能你的问题是你正在使用这样的东西

 // this only bind click handler to existing elements matching selector $('#mycontainer a').click(function(){...}) 

并且您需要使用on来覆盖动态添加的元素。

 // this bind click handler to current and future elements matching selector $('#mycontainer').on('click','a',(function(){...}) 

当您的页面最初加载时,您的点击事件将绑定到您的链接。 这意味着在此之后引入DOM的任何新链接都没有绑定到它们的click事件。 您需要将click事件绑定到要附加的链接:

 $(newlink).click(function(event){ // your onclick code }); $(mydiv).append(newlink);