jQuery .live()和.on()有什么区别 [英] What's the difference between jQuery .live() and .on()

查看:138
本文介绍了jQuery .live()和.on()有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到jQuery 1.7中有一个新方法.on(),它替换了早期版本中的.live().

I see there's a new method .on() in jQuery 1.7 that replaces the .live() in earlier versions.

我想知道它们之间的区别以及使用这种新方法的好处.

I'm interested to know the difference between them and what the benefits are of using this new method.

推荐答案

文档为什么您不想使用现场直播.同样如Felix所提到的,.on是附加事件的更简化的方式.

It's pretty clear in the docs why you wouldn't want to use live. Also as mentioned by Felix, .on is a more streamline way of attaching events.

不再建议使用.live()方法,因为稍后 jQuery版本提供了更好的方法,没有 缺点.特别是,使用 .live():

Use of the .live() method is no longer recommended since later versions of jQuery offer better methods that do not have its drawbacks. In particular, the following issues arise with the use of .live():

  • jQuery尝试在调用.live()方法之前检索选择器指定的元素,这可能是 在大型文档上很耗时.
  • 不支持链接方法.例如,$("a").find(".offsite, .external").live( ... ); 无效有效,并且无法按预期工作.
  • 由于所有.live()事件都附加在document元素上,因此事件花费的时间最长,最慢 处理之前的可能路径.
  • 调用 event.stopPropagation() 在事件处理程序中无法有效地停止事件处理程序 附在文件的下方;该事件已经传播到 document.
  • .live()方法与其他事件方法以令人惊讶的方式进行交互,例如, $(document).unbind("click")删除所有点击处理程序 通过任何对.live()
  • jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.
  • Chaining methods is not supported. For example, $("a").find(".offsite, .external").live( ... ); is not valid and does not work as expected.
  • Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.
  • Calling event.stopPropagation() in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to document.
  • The .live() method interacts with other event methods in ways that can be surprising, e.g., $(document).unbind("click") removes all click handlers attached by any call to .live()!

这篇关于jQuery .live()和.on()有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆