菜鸟求教,请问下面两种jq事件写法有什么区别?

2025-03-17 02:42:38
推荐回答(1个)
回答1:

没区别,click是bind("click")的快捷方式,click的实现就是bind("click")

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 ) {

// Handle event binding
jQuery.fn[ name ] = function( data, fn ) {
return arguments.length > 0 ?
this.on( name, null, data, fn ) :
this.trigger( name );
};
});

源码里可以看到,类似的还有blur focus mousedown之类的,现在的版本都用on了,而非bind,道理都一样的