详解javascript跨浏览器事件处理程序
本文为大家分享了javascript跨浏览器事件处理机制,供大家参考,具体内容如下
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>跨浏览器的事件处理程序</title> </head> <body> <input type="button" value="click me" id="myBtn"/> <input type="button" value="解除" id="unlisten"/> <script> function $(id){ return document.getElementById(id); } var EventUtil={ fnCount:0 ,fnData:{} ,addHandler:function(element,type,handler){ this.fnCount++; handler.fid = this.fnCount; var _fn = handler; handler = function(){ _fn.call(element); }; this.fnData[this.fnCount] = handler; if(element.addEventListener){ element.addEventListener(type,handler,false); } else if(element.attachEvent){ element.attachEvent("on"+type,handler); } else { element["on"+type]=handler; } } ,removeHandler:function(element,type,handler){ handler = this.fnData[handler.fid]; if(element.removeEventListener){ element.removeEventListener(type,handler,false); }else if(element.detachEvent){ element.detachEvent("on"+type,handler); }else{ element["on"+type]=null; } } } var btn=$("myBtn"); var unbtn = $('unlisten'); var bindFn1=function(){ alert(this.id); }; var bindFn2=function(){ alert('2'); }; EventUtil.addHandler(btn,"click",bindFn1); EventUtil.addHandler(btn,"click",bindFn2); //EventUtil.removeHandler(btn,"click",bindFn1); EventUtil.addHandler(unbtn,"click",function(){ EventUtil.removeHandler(btn,"click",bindFn1); }); </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐
Kakoola 2020-08-01
bearhoopIT之道 2020-08-16
凯哥Java 2020-07-04
haidaoxianzi 2020-07-04
delmarks 2020-06-28
aNian 2020-06-16
qingjiuquan 2020-06-07
fraternityjava 2020-06-04
明瞳 2020-06-04
HappyHeng 2020-05-28
88473166 2020-05-14
zkwgpp 2020-05-07
oXiaoChong 2020-05-07
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
xuMelon 2020-05-04
MrLiar 2020-04-27