Created
March 5, 2012 11:27
-
-
Save icai/1977939 to your computer and use it in GitHub Desktop.
addevent & delevent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addEvent(obj,evt,fn) { | |
if (obj.addEventListener && !Browser.isOpera) { | |
obj.addEventListener(evt,fn,false); | |
return obj; | |
} | |
if (!obj.functions) obj.functions={}; | |
if (!obj.functions[evt]) | |
obj.functions[evt]=[]; | |
//obj.functions["mousedown"]=[] | |
var functions=obj.functions[evt]; | |
for (var i=0;i<functions.length;i++) { | |
if (functions[i]===fn) return obj; | |
} | |
functions.push(fn); | |
//fn.index=functions.length-1; | |
if (typeof obj["on"+evt]=="function") { | |
//alert(obj["on"+evt]);//当这一行执行到时,obj["on"+evt] 就是handler | |
//alert(obj["on"+evt]==handler); | |
if (obj["on"+evt]!=handler) | |
functions.push(obj["on"+evt]); | |
} | |
obj["on"+evt]=handler; | |
return obj; | |
} | |
function delEvent(obj,evtype,fn) { | |
if (obj.removeEventListener && !Browser.isOpera) { | |
obj.removeEventListener(evtype,fn,false); | |
return obj; | |
} | |
var fns=obj.functions || {}; | |
fns=fns[evtype] || []; | |
for (var i=0;i<fns.length;i++) { | |
if (fns[i]==fn) { | |
fns.splice(i,1); | |
return obj; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment