首页 > 代码库 > javascript事件之:jQuery.event.remove事件详解

javascript事件之:jQuery.event.remove事件详解

  之前已经介绍过jQuery.event.add,现在我们来看看jQuery.event.remove

  先上代码

  

/*    elem: 处理的元素    types: 移除的事件    handler: 移除的方法    selector: 委托的元素    mappedTypes:*/remove: function( elem, types, handler, selector, mappedTypes ) {    var j, origCount, tmp,        events, t, handleObj,        special, handlers, type, namespaces, origType,        //得到绑定在elem上的data缓存        elemData = http://www.mamicode.com/data_priv.hasData( elem ) && data_priv.get( elem );    // 没有elemData或者elemData.events,退出函数    if ( !elemData || !(events = elemData.events) ) {        return;    }    // 处理用空格分割开的多事件操作    types = ( types || "" ).match( core_rnotwhite ) || [""];    t = types.length;    while ( t-- ) {        //typenamespace = /^([^.]*)(?:\.(.+)|)$/;        tmp = rtypenamespace.exec( types[t] ) || [];        type = origType = tmp[1];        namespaces = ( tmp[2] || "" ).split( "." ).sort();        // 移除该元素下所有事件        if ( !type ) {            for ( type in events ) {                jQuery.event.remove( elem, type + types[ t ], handler, selector, true );            }            continue;        }        special = jQuery.event.special[ type ] || {};        type = ( selector ? special.delegateType : special.bindType ) || type;        handlers = events[ type ] || [];        //如果存在命名空间,得到一个带命名空间的正则        tmp = tmp[2] && new RegExp( "(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)" );        origCount = j = handlers.length;        while ( j-- ) {            //得到handlers下的一个handleObj            handleObj = handlers[ j ];            if ( ( mappedTypes || origType === handleObj.origType ) &&                ( !handler || handler.guid === handleObj.guid ) &&                ( !tmp || tmp.test( handleObj.namespace ) ) &&                ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) {                // 移除handlers下对应的事件对象                handlers.splice( j, 1 );                // 处理delegateCount                if ( handleObj.selector ) {                    handlers.delegateCount--;                }                // 如果specia下有remove,调用                if ( special.remove ) {                    special.remove.call( elem, handleObj );                }            }        }        // Remove generic event handler if we removed something and no more handlers exist        // (avoids potential for endless recursion during removal of special event handlers)        if ( origCount && !handlers.length ) {            if ( !special.teardown || special.teardown.call( elem, namespaces, elemData.handle ) === false ) {                jQuery.removeEvent( elem, type, elemData.handle );            }            // 移除evens下类型            delete events[ type ];        }    }    // Remove the expando if it‘s no longer used    if ( jQuery.isEmptyObject( events ) ) {        delete elemData.handle;        data_priv.remove( elem, "events" );    }},

 

javascript事件之:jQuery.event.remove事件详解