• 另一个版本的addEvent与removeEvent


    主要是在内存泄漏上的改进

    (function() {
    	window.EventUtils = {
    		eventFuncs : [],
    
    		addEvent : function(o, n, f) {
    			var el, id;
    
    			// Resolve element by id if needed
    			o = typeof(o) == 'string' ? document.getElementById(o) : o;
    
    			if (o.attachEvent) {
    				// Since we can't use attachEvent we need to generate an unique id for the object
    				// and place functions in an array one for each object
    				el = EventUtils.eventFuncs;
    				id = o._evtID;
    
    				if (!el[id]) {
    					// Generate new unique id
    					id = o._evtID = el.length;
    
    					// Add event listener old fashion way instead of attachEvent
    					o['on' + n] = function() {
    						var i, l, e = window.event, li;
    
    						e.target = e.srcElement; // Force W3C style
    
    						// Execute each event listener in order
    						for (i = 0, li = el[e.target._evtID], l = li.length; i < l; i++)
    							li[i](e);
    					};
    
    					// Create array with first function
    					el[id] = [f];
    				} else
    					el[id].push(f); // Push in more functions
    
    				// Fix the IE leak
    				o = null;
    			} else if (o.addEventListener)
    				o.addEventListener(n, f, false);
    			else
    				o['on' + n] = f;
    
    			return f;
    		},
    
    		removeEvent : function(o, n, f) {
    			var i, li;
    
    			// Resolve element by id if needed
    			o = typeof(o) == 'string' ? document.getElementById(o) : o;
    
    			if (o.detachEvent) {
    				li = EventUtils.eventFuncs[o._evtID];
    
    				if (li) {
    					// Detach event listener by looking for it and remove it from the array
    					for (i = 0; i < li.length; i++) {
    						if (li[i] === f)
    							li.splice(i, 1);
    					}
    				}
    			} else if (o.removeEventListener)
    				o.removeEventListener(n, f, false);
    			else
    				o['on' + n] = null;
    		}
    	};
    })();
    

  • 相关阅读:
    java读取jar包中的文件
    mysql 常用命令搜集
    如何终端自动导入cer开发证书到钥匙串
    解决第三方库私有api造成的apple审核不通过。
    push证书过期,不需要升级客户端。
    mac下面新建qq(多开/打开多个)登录方法
    行动力才是王道
    wordpress | 网站访问速度优化方案(Avada)
    HTML | video的封面平铺方法
    PHP | 获取数组长度的方法
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/1634739.html
Copyright © 2020-2023  润新知