<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box">
<input type="button" value="按钮" id="btn">
</div>
<script>
var btn=getElementById("btn"); //获取id
function showMes() { //执行的函数
alert("hello world!");
}
var eventUtil={
//添加句柄
addHandler:function(element,type,handler){
if(element.attachEvent){
element.attachEvent('on'+type,handler);
}else if(element.addEventListener){
element.addEventListener(type,handler,false);
}else{
element['on'+type]=handler;
}
},
//删除句柄
removeHandler:function(element,type,handler){
if(element.detachEvent){
element.detachEvent('on'+type,handler);
}else if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}else{
element['on'+type]=null;
}
}
}
eventUtil.addHandler(btn,'click',showMes); //执行添加句柄函数
//eventUtil.removeHandler(btn,'click',showMes); //执行删除句柄函数
</script>
</body>
</html>