<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>事件流</title>
</head>
<body>
<div>
<input type="button" value="按钮" id="btn">
</div>
<script type="text/javascript">
function shoeMes(){
alert('hello world!');
}
var btn=document.getElementById('btn');
/* btn.onclick=function(){
alert('这是通过DOM0级添加的事件!');
} */
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'+click]=handler;
}
},
//删除句柄
removeHandler:function(element,type,handler){
if(element.addEventListener){
element.removeEventListener(type,handler,false);
}else if(element.attachEvent){
element.detachEvent('on'+type,handler);
}else{
element['on'+click]=null;
}
}
}
eventUtil.addHandler(btn,'click',showMes);
</script>
</body>
</html>
删除判断的条件是removeEventListener和detachEvent