慕斯卡9731973
2016-04-09 18:28
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="box">
<input type="button" value="按钮2" id="btn2">
<input type="button" value="按钮3" id="btn3">
</div>
<script type="text/javascript">
function showMsg(){
alert("Hello World!");
}
var btn2 = document.getElementById("btn2");
var btn3 = document.getElementById("btn3");
btn2.onclick=function(){
alert("fuck you");
};
btn2.onclick=null;
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;
}
}
};
eventUtil.addHandler("btn3",'click',showMsg);
</script>
</body>
</html>
倒数第四行 eventUtil.addHandler("btn3",'click',showMsg);改成eventUtil.addHandler(btn3,'click',showMsg);就ok了
DOM事件探秘
99544 学习 · 1197 问题
相似问题