.live()已经弃用!

原文:http://api.jquery.com/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():


使用.live()方法已经被丢弃,因为新版jQuery提供了更好的方法来避免.live()方法的缺点。.live()方法会引起以下问题:

jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.

1) 调用.live()方法之前,jQuery会尝试根据选择器去检索元素,在大文档中这是相当耗时的。

Chaining methods is not supported. For example, $("a").find(".offsite, .external").live( ... ); is not valid and does not work as expected.

2) 链式方法不被支持。如 $("a").find(".offsite, .external").live( ... ); 是无效的,而且不能按预期工作。

Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.

3) 由于所有的.live()事件都被绑定到document对象中,事件在执行之前将经过最长的并且最慢的路径。

On mobile iOS (iPhone, iPad and iPod Touch) the click event does not bubble to the document body and cannot be used with .live() without applying one of the following workarounds:

4) 在iOS系统中,click事件不能冒泡到document.body,所以不使用以下解决方法不能使用.live():

Use natively clickable elements such as a or button , as both of these options do bubble correctly.

4.1) 使用原生的可点击的元素,如 a 或 button;

Alternatively, use .on() (or .delegate() ) as the click event can be handled below the level of the
document.body . Since mobile iOS does bubble here, it can also be considered a reliable alternative.

4.2) 另外,在document.body下使用 .on()(或 .delegate())作为点击事件可被处理(handled)。因为iOS可以冒泡到这里。

Applying the CSS style cursor:pointer to either the element that needs to bubble clicks or a parent (including document.documentElement ) can also work. Note however, that this has negative side-effects such as disabling copy\paste and should be used with caution.

4.3) 对每一个需要冒泡的click或者是父结点 (including document.documentElement ) 应用CSS样式cursor:pointer也可正常运行。注:这会产生副效果,如使copy/paste失效,所以必须谨慎使用。

  • Callingevent.stopPropagation()in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated todocument.

5) 在事件句柄中调用 event.stopPropagation()无法阻止事件句柄被绑定到docuemt下的元素;事件已经传播到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()!

6) .live()方法与其它事件方法关联有时是很奇怪的。如 $(document).unbind("click")删除所有通过.live()绑定的click事件!

For pages still using.live(), this list of version-specific differences may be helpful:


  • Before jQuery 1.7, to stop further handlers from executing after one bound using.live(), the handler must returnfalse. Calling.stopPropagation()will not accomplish this.

  • As ofjQuery 1.4the.live()method supports custom events as well asall JavaScript events that bubble.
  • InjQuery 1.3.xonly the following JavaScript events could be bound:click,dblclick,keydown,keypress,keyup,mousedown,mousemove,mouseout,mouseover, andmouseup.

你可能感兴趣的:(live)