- 时间:2021-09-09 02:32 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:jquery中绑定事件的异同
[b]谈论jquery中bind(),live(),delegate(),on()绑定事件方式[/b]
[b]1. Bind()[/b]
[code]$(selector).bind(event,data,function)[/code]
Event:必须项;添加到元素的一个或多个事件。
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
[code]$(selector).bind({event1:function, event2:function, ...});[/code]
[b]2.live()[/b]
[code]$(selector).live(event,data,function) [/code]
Event:必须项;添加到元素的一个或多个事件
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
[code]$(selector).live({event1:function, event2:function, ...}) [/code]
[b]3.delegate()[/b]
[code]$(selector).delegate(childSelector,event,data,function)[/code]
childSelector:必须项;需要添加事件处理程序的元素,一般为selector的子元素;
event:必须项;添加到元素的一个或多个事件
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
[code]$(selector).delegate(childselector,{event1:function, event2:function, ...})[/code]
[b]4.on()[/b]
[code]$(selector).on(event,childselector,data,function)[/code]
childSelector:必须项;需要添加事件处理程序的元素,一般为selector的子元素;
event:必须项;添加到元素的一个或多个事件
Data:可选;需要传递的参数
Function:必需;当绑定事件发生时,需要执行的函数;
定义事件:
[code]$(selector).on({event1:function, event2:function, ...},childselector); [/code]
[b]四种方式的异同和优缺点[/b]
[b]相同点:[/b]
1.都支持单元多事件的绑定;空格相隔方式或者是大括号替代方式;
2.均是通过事件方式,将事件传递到document进行事件的响应;
[b]比较:[/b]
1.bind()函数只能针对已经存在的元素进行事件的设置;但是live(),on(),delegate(),均支持未来新添加元素的事件设置;
2.bind()函数在jquery1.7版本以前比较受推崇,1.7版本出来之后,官方已经不推荐用bind(),替代函数为on(),这也是1.7版本新添加的函数,同样,可以用来代替live()函数,live()函数在1.9版本已经删除;
3.live()函数和delegate()函数两者类似,但是live()函数在执行速度,灵活性和CSS选择器支持方面较delegate()差些。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程素材网!