原文: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()
method, which may be time-consuming on large documents.
$("a").find(".offsite, .external").live( ... );
is
not
valid and does not work as expected.
.live()
events are attached at the
document
element, events take the longest and slowest possible path before they are handled.
click
event does not bubble to the document body and cannot be used with
.live()
without applying one of the following workarounds:
a
or
button
, as both of these options do bubble correctly.
.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.
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.
document.documentElement
)
应用CSS样式cursor:pointer也可正常运行。注:这会产生副效果,如使copy/paste失效,所以必须谨慎使用。
event.stopPropagation()
in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated todocument
.event.stopPropagation()无法阻止事件句柄被绑定到docuemt下的元素;事件已经传播到document。
.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()
! For pages still using.live()
, this list of version-specific differences may be helpful:
.live()
, the handler must returnfalse
. Calling.stopPropagation()
will not accomplish this..live()
method supports custom events as well asall JavaScript events that bubble.click
,dblclick
,keydown
,keypress
,keyup
,mousedown
,mousemove
,mouseout
,mouseover
, andmouseup
.