jQuery .live()和.on()之间有什么区别?

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

我有兴趣了解他们之间的区别,以及使用这种新方法的好处。

很清楚在docs为什么你不想使用live。也正如Felix所说,.on是一种更流畅的附加事件的方式。

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 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 07001
    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()!
http://stackoverflow.com/questions/8042576/whats-the-difference-between-jquery-live-and-on

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:jQuery .live()和.on()之间有什么区别?