标签:rem bsp window app span else load element type
if(document.addEventListener){ this.addEvent = function(elem,type,fn){ elem.addEventListener(type,fn,false); return fn; }; this.removeEvent = function(elem,type,fn){ elem.removeEventListener(type,fn,false); }; }else if(document.attachEvent){ this.addEvent = function(elem,type,fn){ var bound = function(){ return fn.apply(elem,arguments); }; elem.attachEvent("on" + type,bound); return bound; }; this.removeEvent = function(elem,type,fn){ elem.detachEvent("on" + type,fn); }; }
//用法
addEvent(window,"load",function(){
var elems = document.getElementsByTagName("div");
alert(elems.length);
for(var i = 0; i < elems.length; i++)(function(elem){
var handler = addEvent(elem,"click",function(){
this.style.backgroundColor = this.style.backgroundColor == "" ? "green" : "";
removeEvent(elem,"click",handler);
});
})(elems[i]);
});
标签:rem bsp window app span else load element type
原文地址:http://www.cnblogs.com/halo-yang/p/7909263.html