jquery中绑定事件的异同中绑定事件的异同
谈论谈论jquery中中bind(),live(),delegate(),on()绑定事件方式绑定事件方式
1.. Bind()
$(selector).bind(event,data,function)
Event:必须项;添加到元素的一个或多个事件。
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
$(selector).bind({event1:function, event2:function, ...});
2.live()
$(selector).live(event,data,function)
Event:必须项;添加到元素的一个或多个事件
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
$(selector).live({event1:function, event2:function, ...})
3.delegate()
$(selector).delegate(childSelector,event,data,function)
childSelector:必须项;需要添加事件处理程序的元素,一般为selector的子元素;
event:必须项;添加到元素的一个或多个事件
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
$(selector).delegate(childselector,{event1:function, event2:function, ...})
4.on()
$(selector).on(event,childselector,data,function)
childSelector:必须项;需要添加事件处理程序的元素,一般为selector的子元素;
event:必须项;添加到元素的一个或多个事件
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
$(selector).on({event1:function, event2:function, ...},childselector);
四种方式的异同和优缺点四种方式的异同和优缺点
相同点:相同点:
1.都支持单元多事件的绑定;空格相隔方式或者是大括号替代方式;
2.均是通过事件方式,将事件传递到document进行事件的响应;
比较:比较:
评论0