不能实现效果啊
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<input id="btn" type="button" value="按钮" onclick="showMes()" />
<input id="btn2" type="button" value="按钮2" />
<input id="btn3" type="button" value="按钮3" />
<script type="text/javascript">
function showMes(){
alert("ni hao!");
}
//DOM0级
var btn2=document.getElementById('btn2');
var btn3=document.getElementById('btn3');
btn2.onclick=function(){
alert("ni hao!");
}
//btn2.onclick=null;
/* //DOM2级
btn3.addEventListener('click',showMes,false);
//btn3.removeEventListener('click',showMes,false);*/
//IE
// btn3.attachEvent('onclick',showMes);
//btn3.detachEvent('onclick',showMes);*/
var eventUtil={
addHadler:function(element,type,handler){
if(element.addEventListener){ //DOM2级
element.addEventListener(type,handler,false);
}else if(element.attachEvent){ //IE
element.attachEvent('on'+type,handler);
}else{ //DOM0级
element['on'+type]=handler;
}
},
removeHadler:function(element,type,handler){
if(element.removeEventListener){ //DOM2级
element.removeEventListener(type,handler,false);
}else if(element.detachEvent){ //IE
element.detachEvent('on'+type,handler);
}else{ //DOM0级
element['on'+type]=null;
}
}
}
eventUtil.addHandler(btn3,'click',showMes);
</script>
</body>
</html>