var oldTrigger = jQuery.event.trigger;
jQuery.event.trigger = function(event, data, elem) {
// do stuff
oldTrigger.apply(this, arguments);
}
只需要仔细检查每个触发器是否都通过此方法。
trigger: function( type, data ) {
return this.each(function() {
jQuery.event.trigger( type, data, this );
});
},
至少$.fn.trigger
通过 jQuery.event.trigger
// Trigger the event, it is assumed that "handle" is a function
var handle = jQuery.data( elem, "handle" );
if ( handle ) {
handle.apply( elem, data );
}
或者你可以覆盖jQuery.data
并拦截事件处理程序的设置。
for ( var i = 0, l = this.length; i < l; i++ ) {
jQuery.event.add( this[i], type, handler, data );
}
The bind
方法用途jQuery.event.add
添加处理程序。您也可以拦截它。
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").split(" "), function( i, name ) {
// Handle event binding
jQuery.fn[ name ] = function( fn ) {
return fn ? this.bind( name, fn ) : this.trigger( name );
};
if ( jQuery.attrFn ) {
jQuery.attrFn[ name ] = true;
}
});
所有标准事件都会经过$.fn.trigger
or $.fn.bind
if ( name === "live" ) {
// bind live handler
context.each(function(){
jQuery.event.add( this, liveConvert( type, selector ),
{ data: data, selector: selector, handler: fn, origType: type, origHandler: fn, preType: preType } );
});
$.fn.live
穿过去jQuery.event.add
.
总之,一切都会过去jQuery.event.add
依次执行以下操作:
if ( !eventHandle ) {
elemData.handle = eventHandle = function() {
// Handle the second event of a trigger and when
// an event is called after a page has unloaded
return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
jQuery.event.handle.apply( eventHandle.elem, arguments ) :
undefined;
};
}
正在将事件处理程序写入jQuery.data( elem )
where elem
是一个 DOM 节点。
[[综上所述]]
我个人会说jQuery.data
很复杂并且安全地覆盖很痛苦,所以只需覆盖jQuery.event.add
.
[[实例]]
http://jsfiddle.net/qLRuT/8/ http://jsfiddle.net/qLRuT/8/