比如ul里面有6个li
bind: this.on( types, null, data, fn );
$('li').bind('click',fn)
- 直接绑定到li元素上,效率较低,因为要循环6次进行绑定
- 通过在
fn
中查看this
得知,不会动态绑定到新加的元素上,如若元素较多则效率不高。
live:live(type, [data], fn)
$('li').live('click',fn),
- 直接将元素绑定到
$(document)
对象上,只绑定一次- 会动态绑定到新加的元素上
- 不能使用
event.stopPropagation()
停止事件冒泡,且事件需要传传播到document
上,效率不高。从1.7版本后废除。
delegate:delegate(selector,type,[data],fn)
$('ul').delegate('li','click',fn);
- 直接将元素绑定到调用对象上,比live灵活且比live性能好,因为减少了查询次数,
live
从document
上开始查找的,delegate
可指定委托对象,也只绑定一次,会动态绑定到新加的元素上
on,前三种方法都依赖on
on实现:on(type,[selectors],[data],fn);
- bind:
this.on(type,null,data,fn)
- live中有type,
type
只能为document
,- delegate:
this.on( types, selector, data, fn );