<body><input type="button" value="按钮" id="btn" onclick="showMessage()"/><input type="button" value="按钮2" id="btn2" /><input type="button" value="按钮3" id="btn3" /><script>function showMessage(){ alert('hallo world');}var btn2=document.getElementById('btn2');var btn3=document.getElementById('btn3');var eventUtil={addHandler:function(element,type,handler){ if(element.addEventListener) {elemnet.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) {elemnet.removeEventListener(type,handler,false);}else if(element.detachEvent){element.detachEvent('on'+type,handler);}else{element['on'+type]=null;}}}eventUtil.addHandler(btn3,'click',showMessage);</script></body>
1 回答
已采纳
六道骸
TA贡献30条经验 获得超6个赞
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
提交
取消