首页 > Web开发 > 详细

jquery live 区别

时间:2015-01-24 20:00:26      阅读:323      评论:0      收藏:0      [点我收藏+]

http://www.360doc.com/content/13/1222/22/14022539_339358149.shtml

开始的时候在jQuery.1.7.1中使用了.live()觉得很好用,特别是在绑定事件之后再加入的元素的事件绑定上很方便(第一次live之后以后添加的元素就不需要绑定啦)

后来jQuery更新到1.9.1,页面中的.live报错:"has no method live", 后来查了文档才知道在新版本中做了修改。

jQuery.1.8.1:

$("#liveID").live("click",function(){alert("live click");});

jQuery.1.9.1:

$(document).on("click","#liveID",function(){alert("live click");});

 

jQuery网站上这么说的:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page. See the discussion of direct versus delegated events in the.on() method for more information.

改进后的使用建议:

1 $(selector).live(events, data, handler); // jQuery 1.3+

2 $(document).delegate(selector, events, data, handler); // jQuery 1.4.3+

3 $(document).on(events, selector, data, handler); // jQuery 1.7+

示例:
1 $("a.offsite").live("click", function(){ alert("Goodbye!"); }); // jQuery 1.3+

2 $(document).delegate("a.offsite", "click", function(){ alert("Goodbye!"); }); // jQuery 1.4.3+

3 $(document).on("click", "a.offsite", function(){ alert("Goodbye!"); }); // jQuery 1.7+

jquery live 区别

原文:http://www.cnblogs.com/jcz1206/p/4246335.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!