<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <input id="btn1" type="button" value="按钮1"> <input id="btn2" type="button" value="按钮2"> <input id="btn3" type="button" value="按钮3"> <input id="btn4" type="button" value="按钮4"> <script> function showMes(){ alert("hello world!"); } var btn2=document.querySelector("#btn2"); var btn3=document.querySelector("#btn3"); var btn4=document.querySelector("#btn4"); var eventUtl={ addHandler:function(element,type,handler){ if(element.addEventListener){ //判断如果是dom2级事件处理对象,就采用dom2级 element.addEventListener(type,handler,false); }else if(element.attachEvent){ //判断如果是IE事件处理对象,就采用IE element.attachEvent("on"+type,handler); }else{ //判断如果是dom0级事件处理对象,就采用dom0级 element["on"+type]=handler; } } removeHandler:function(element,type,handler){ if(element.removeEventListener){ element.removeEventListener(type,handler,false); }else if(element.detachEvent){ element.detachEvent("on"+type,handler); }else{ element["on"+type]=null; } } } eventUtl.addHandler(btn4,"click",showMes); eventUtl.removeHandler(btn4,"click",showMes); </script> </body> </html>
大神请看,我这行代码出错了,但是不知道原因是啥,麻烦帮我看看
千秋此意
相关分类