• 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 = 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" );
        }
    },
  • 相关阅读:
    图解集合5:不正确地使用HashMap引发死循环及元素丢失
    图解集合4:HashMap
    图解集合3:CopyOnWriteArrayList
    图解集合2:LinkedList
    SharePoint PowerShell 修改母版页
    SharePoint PowerShell 启动工作流
    SharePoint REST 服务获取讨论版问题
    SharePoint 前端开发常用的对象之_spPageContextInfo
    SharePoint 读取内容的插件之SharepointPlus
    SharePoint 配置PowerShell任务计划
  • 原文地址:https://www.cnblogs.com/pfzeng/p/4189396.html
Copyright © 2020-2023  润新知