<!DOCTYPE html><html><head> <title>20160217</title></head><body> <input type="button" value="按钮" id="btn"> <script type="text/javascript"> var eventUtil={ addHandler:function(element,type,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){ if (element.removeEventListener){ element.removeEventListener(type,handler,false); }else if(element.detachEvent){ element.detachEvent("on"+type,handler); }else { element["on"+type]=null; } } } var btn=document.getElementById("btn"); eventUtil.addHandler(btn,"click",show); function show(){ alert("hello"); } </script></body></html>
1 回答
李晓健
TA贡献1036条经验 获得超461个赞
var eventUtil={ addHandler:function(element,type,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){ if (element.removeEventListener){ element.removeEventListener(type,handler,false); }else if(element.detachEvent){ element.detachEvent("on"+type,handler); }else { element["on"+type]=null; } } }
添加回答
举报
0/150
提交
取消