我看到jQuery1.7中有一个新的方法.on()
,它取代了早期版本中的.live()
.
I'm interested to know the difference between them and what the benefits are of using this new method.
我看到jQuery1.7中有一个新的方法.on()
,它取代了早期版本中的.live()
.
I'm interested to know the difference between them and what the benefits are of using this new method.
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.
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()
方法之前try 检索 Select 器指定的元素,该方法可能是- Chaining methods is not supported. For example,
$("a").find(".offsite, .external").live( ... );
is not valid and does not work as expected.- 由于所有
.live()
个事件都附加在document
元素上,因此事件花费的时间最长、速度最慢- Calling
event.stopPropagation()
in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated todocument
..live()
方法以令人惊讶的方式与其他事件方法交互,例如,$(document).unbind("click")
删除所有单击处理程序 任何拨打.live()
的电话都可以!