比如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
上,效率不高。
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
中没有type
直接返回this.on(type,null,data,fn)
live
中有type,type
只能为document
,剩下的就是delegate,delegate和on
的差别是形参顺序不同。