【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节点上面.在他们触发之前需要耗费很多时间和资源]

转载于:https://www.cnblogs.com/dongguapifly/p/10627158.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值