【jquery】jquery绑定事件的方式 bind() delegate() live()以及on()

之前知道一些jquery的绑定方法没怎么区分:

看了api文档,有些地方仍然是看的云里雾里简单的总结一下

1, bind()方法 

对应的解除绑定方法unbind()

只需要明白这一点:动态生成的元素是不会被绑定事件的

2,delegate()方法

对应的解除绑定方法undelegate()

3,live()方法

对应的解除绑定方法die()       该绑定方式1.9版本正式被移除,请注意版本化差异

4,on()方法

对应的解除绑定方法是off() ,官方建议使用这个绑定事件,但是貌似底层仍然用的是delegate方法

除了bind()方法 下面的三个方法都可以对动态添加的元素绑定事件.live()方法的一些缺点:


 最白话的一种:live()效率不高··

下面是官方文档ive()的缺点:我就随便翻译下了··· 

  • jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.  [翻译:在调用live()方法绑定事件之前,jquery去查找符合特定选择器的元素这一个过程耗费在大量的dom元素上的时间可能是特别大]
  • Chaining methods is not supported. For example, $( "a" ).find( ".offsite, .external" ).live( ... );  is notvalid and does not work as expected. [翻译:链式法则是不起作用的 .比如$( "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.[翻译:因为所有的live()方法绑定事件都是绑定到根dom节点上面.在他们触发之前需要耗费很多时间和资源]

posted on 2015-03-11 23:13  狂奔的冬瓜  阅读(139)  评论(0编辑  收藏  举报