兼容的地方,IE仍然没效果,帮忙看看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM事件对象</title>
</head>
<body>
<div>
<input type="button" value="按钮1" id="button1" onclick="showMes()" />
<input type="button" value="按键2" id="button2" />
<input type="button" value="按钮3" id="button3" />
</div>
<script type="text/javascript">
function showMes(){
event = event || window.event;
alert(event.type);
alert(event.target.nodeName);
}
var button2 = document.getElementById('button2');
var button3 = document.getElementById('button3');
button2.onclick = function(){
alert("DOM0级事件处理程序");
}
button2.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{
//DOM0级,老版本浏览器
//element[onclick] === element.onclick
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(button3,'click',showMes);
</script>
</body>
</html>