<html>
<head>
<title>事件流</title>
<meta charset="utf-8">
<head>
<body>
<div id="box">
<input type="button" value="按钮" id="btn" onclick="showMes()">
<input type="button" value="按钮1" id="btn1">
<input type="button" value="按钮2" id="btn2">
</div>
<script>
function showMes(){
alert("hello world!");
}
var btn1=document.getElementById("btn1");
var btn2=document.getElementById("btn2");
var btn3=document.getElementById("btn3");
btn1.onclick=function(){
alert("DOM0级添加的事件!");
}
btn1.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(btn2,'click',showMes);
</script>
</body>
</html>我没加removeHandler就可以用,但是加了以后我的按钮2就什么也弹不出来了,我明明还没有调用,请问是怎么回事啊?
添加回答
举报
0/150
提交
取消