Javascript 为所有事件类型附加处理程序

Javascript 为所有事件类型附加处理程序,javascript,javascript-events,jquery,Javascript,Javascript Events,Jquery,是否有一种简洁的方法将事件处理程序绑定到元素,而不考虑事件类型 例如,我需要以下内容: $('#elem').bind(function(e){ //do stuff for any event originating from this element e.stopPropagation(); }); 你可以在这里找到事件列表 你可以在这里找到事件列表 您可以更改bind方法的默认行为。 如果我们给出一个参数,这个参数将函数添加到所有事件中 看看这个 $.fn.init.p

是否有一种简洁的方法将事件处理程序绑定到元素,而不考虑事件类型

例如,我需要以下内容:

$('#elem').bind(function(e){

    //do stuff for any event originating from this element
    e.stopPropagation();
});
你可以在这里找到事件列表


你可以在这里找到事件列表

您可以更改
bind
方法的默认行为。 如果我们给出一个参数,这个参数将函数添加到所有事件中 看看这个

$.fn.init.prototype.bind  = ( function( old ) {  

     return function( ) { 
        if( arguments.length == 1 && typeof arguments[0] === "function" ) { 
          // if we'll put to bind method one argument which is function 

          // list of whole of events - you set themselves as needed
          return this.bind( "click keydown keyup keypress mouseover mouseenter  mouseout mouseleave mousedown mouseup mousemove change blur focus scroll resize load unload beforeunload", arguments[0] );

        } else { 
            // trigger original bind method
            return old.apply( this, arguments ) 
        } ;
     }  

})( $.fn.init.prototype.bind );
现在:

$( "body" ).bind( function() { console.log( this ) } );

Poof:)

您可以更改
bind
方法的默认行为。 如果我们给出一个参数,这个参数将函数添加到所有事件中 看看这个

$.fn.init.prototype.bind  = ( function( old ) {  

     return function( ) { 
        if( arguments.length == 1 && typeof arguments[0] === "function" ) { 
          // if we'll put to bind method one argument which is function 

          // list of whole of events - you set themselves as needed
          return this.bind( "click keydown keyup keypress mouseover mouseenter  mouseout mouseleave mousedown mouseup mousemove change blur focus scroll resize load unload beforeunload", arguments[0] );

        } else { 
            // trigger original bind method
            return old.apply( this, arguments ) 
        } ;
     }  

})( $.fn.init.prototype.bind );
现在:

$( "body" ).bind( function() { console.log( this ) } );

Poof:)

这个问题有一个类似的答案:谢谢,不知怎么的,当我发布这个问题时,在“类似问题”部分中没有出现这个答案。:)无论如何,我的问题是“整洁”,我仍然想知道是否还有其他方法可以做到这一点。这个问题有一个类似的答案:谢谢,不知怎的,在我发布这篇文章时,“类似问题”部分没有显示这一点。:)无论如何,我的问题是“整洁”,我仍然想知道是否还有其他方法可以做到这一点。你在插件开发人员中看起来像rockstar:)你在插件开发人员中看起来像rockstar:)