
事件绑定的方法有很多种,使用了jquery那么原理那种绑定方式(elem.click = function(){…}))就不太想推荐给大家了。最主要的原因是elem.click=fn这种方式只能绑定一个事件处理,多次绑定的只会保留最后一次绑定的结果。
下面给大家介绍jquery绑定事件的方式有哪些吧。
jQuery.fn.eventType([[data,] fn])
比如eventType指的是事件类型,比如click: $(“#chua”).click(fn);
data这个参数一般都不会使用。这种方式事件绑定在(“#chua”)上,没有委托事件,和js原生的事件绑定更接近。我们看一下源码
jQuery.each( (“blur focus focusin focusout load resize scroll unload click dblclick ”
“mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave ”
“change select submit keydown keypress keyup error contextmenu”).split(” “), function( i, name ) {
//合并15种事件统一增加到jQuery.fn上,内部调用this.on / this.trigger
jQuery.fn[ name ] = function( data, fn ) {
return arguments.length
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...