jQuery - how to use the “on()” method instead of “live()”? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-26 20:49:40

问题


This question already has an answer here:

  • Turning live() into on() in jQuery 5 answers

I used live() for generated pages and frames. But in jQuery 1.9 this function is deprecated and does not work.

I use on() instead of live() but this method works for one time, and does not work in frames.

My code looks like this:

  $("#element").live('click',function(){
    $("#my").html(result);
   });

What is the solution?


回答1:


$('body').on('click', '#element', function(){
    $("#my").html(result);
});

The clicked element selector is now passed through the .on() function parameters, and the previous selector should be replaced with the closest parent selector preferably with an ID. If you do not know what parent selector to use, body works too, but is less efficient.

see jQuery 1.9 .live() is not a function on how to migrate existing code.



来源:https://stackoverflow.com/questions/14703646/jquery-how-to-use-the-on-method-instead-of-live

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!