首页 > 代码库 > 使用live delegate on来解决js对后加载的html失效的问题
使用live delegate on来解决js对后加载的html失效的问题
今天要写一个前端的东西。每多学一点知识,就可以少写几行代码、几十行代码、几个文件量的代码……这是真的。一直对前端的研究都是停留在遇到问题百度谷歌答案的做法。今天遇到这样一个问题:已经写好的js代码,对ajax加载的html失效了。
用好几组关键字都没有找到答案,最后这组帮我找到了解答的办法:
很幸运,我来到了:http://stackoverflow.com/questions/10161938/jquery-function-not-working-on-ajax-loaded-content
将原来写的
jQuery(‘.ajaxCommentMiniList a‘).click(function(){
alert(123);
return false;
});
改成:
jQuery(‘.ajaxCommentMiniList a‘).live(‘click‘, function(){
alert(123);
return false;
});
即可。网上不少文章建议使用delegate而不是live,甚至还有on。
http://stackoverflow.com/questions/4579117/jquery-live-vs-delegate
$(‘#containerElement‘).on(‘click‘, ‘a.myClass‘, function() { ... });
看来on是更好的解决方案。于是我最后的代码改为:
找个时间研究一下:live VS delegate VS on。希望本文帮到部分遇到与我同样问题的朋友。
用好几组关键字都没有找到答案,最后这组帮我找到了解答的办法:
javascript not work on ajax content
很幸运,我来到了:http://stackoverflow.com/questions/10161938/jquery-function-not-working-on-ajax-loaded-content
有经验的人回答了:
When you initially created the .click() handler, that second button was not yet in the DOM, so jQuery could not attach an event handler to it. The .live() function will work for any element that is already in the DOM and also any element that will be added in the future (e.g. from an ajax call).
将原来写的
jQuery(‘.ajaxCommentMiniList a‘).click(function(){
alert(123);
return false;
});
改成:
jQuery(‘.ajaxCommentMiniList a‘).live(‘click‘, function(){
alert(123);
return false;
});
即可。网上不少文章建议使用delegate而不是live,甚至还有on。
http://stackoverflow.com/questions/4579117/jquery-live-vs-delegate
This, however, seems to me to be much more explicit about what is actually happening. You don‘t realise from the live example that the events are actually being captured on document; with delegate, it is clear that the event capturing happens on #containerElement. You can do the same thing with live, but the syntax becomes increasingly horrid.
Specifying a context for your events to be captured also improves performance. With the live example, every single click on the entire document has to be compared with the selector a.myClass to see if it matches. With delegate, that is only the elements within #containerElement. This will obviously improve performance.
Finally, live requires that your browser looks for a.myClass whether or not it currently exists. delegate only looks for the elements when the events are triggered, giving a further performance advantage.
NB delegate uses live behind the scenes, so you can do anything with live that you can do with delegate. My answer deals with them as they are commonly used.
Note also that neither live nor delegate is the best way to do event delegation in modern jQuery. The new syntax (as of jQuery 1.7) is with the on function. The syntax is as follows:
$(‘#containerElement‘).on(‘click‘, ‘a.myClass‘, function() { ... });
看来on是更好的解决方案。于是我最后的代码改为:
jQuery(‘.ajaxCommentMiniList‘).on(‘click‘, ‘.pagination a‘,function(){
ajaxCommentMiniList.clickAction(jQuery(this)); return false; });
找个时间研究一下:live VS delegate VS on。希望本文帮到部分遇到与我同样问题的朋友。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。