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进行事件的响应; 比较: 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()差些。 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持wanshiok.com! jquery,绑定事件
|