标签:
jQuery.event.dispatch,字面意思是事件分发。就是执行绑定的函数。
在jQuery.event.add中,
//1, jQuery.event.add中 jQuery.event.dispatch.apply( eventHandle.elem, arguments ) //2, jQuery.event.simulate中 jQuery.event.dispatch.call( elem, e );
我们来看看jQuery.event.dispatch的源码,看看其实现了什么。
dispatch: function( event ) { // 修正事件源对象 event = jQuery.event.fix( event ); var i, j, ret, matched, handleObj, handlerQueue = [], args = core_slice.call( arguments ), //得到绑定的事件对象 handlers = ( data_priv.get( this, "events" ) || {} )[ event.type ] || [], special = jQuery.event.special[ event.type ] || {}; args[0] = event; event.delegateTarget = this; // Call the preDispatch hook for the mapped type, and let it bail if desired if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) { return; } // 得到handler的执行队列 handlerQueue = jQuery.event.handlers.call( this, event, handlers ); // Run delegates first; they may want to stop propagation beneath us i = 0; while ( (matched = handlerQueue[ i++ ]) && !event.isPropagationStopped() ) { event.currentTarget = matched.elem; j = 0; while ( (handleObj = matched.handlers[ j++ ]) && !event.isImmediatePropagationStopped() ) { //不存在命名空间,或者匹配的命名空间 if ( !event.namespace_re || event.namespace_re.test( handleObj.namespace ) ) { event.handleObj = handleObj; event.data = handleObj.data; //执行绑定函数 ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler ) .apply( matched.elem, args ); // 如果是return false, 阻止默认事件,阻止冒泡 if ( ret !== undefined ) { if ( (event.result = ret) === false ) { event.preventDefault(); event.stopPropagation(); } } } } } // Call the postDispatch hook for the mapped type if ( special.postDispatch ) { special.postDispatch.call( this, event ); } return event.result; },
javascript事件之:jQuery.event.dispatch详解
标签:
原文地址:http://www.cnblogs.com/pfzeng/p/4195045.html