按鈕5和6都沒反應
<!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=utf-8" />
<title>事件</title>
</head>
<body>
<input type="button" value="按鈕1" onclick="alert('111')"/>
<input type="button" value="按鈕2" onclick="aaa()"/>
<input type="button" value="按鈕3" id="but3"/>
<input type="button" value="按鈕4" id="but4"/>
<input type="button" value="按鈕5" id="but5"/>
<input type="button" value="按鈕6" id="but6"/>
<script type="text/javascript">
//html方法
function aaa(){
alert("222");
}
//DOM0級
var but3=document.getElementById("but3");
but3.onclick=function(){alert("333");};
//DOM2級
var but4=document.getElementById("but4");
but4.addEventListener("click",aaa,false);
//but4.removeEventListener("click",aaa,false);
//IE
var but5=document.getElementById("but5");
but5.attachEvent("onclick",aaa);
//but5.detachEvent("onclick",aaa);
//跨瀏覽器
var eventUtil={
addevent:function(ele,type,hander){
if(ele.addEventListener){
ele.addEventListener(type,hander,false);
}
else if(ele.attachEvent){
ele.attachEvent("on"+type,hander);
}
else{
ele["on"+type]=hander;
//ele.onclick===ele["onclick"];
}
},
removeevent:function(ele,type,hander){
if(ele.removeEventListener){
ele.removeEventListener(type,hander,false);
}
else if(ele.detachEvent){
ele.detachEvent("on"+type,hander);
}
else{
ele["on"+type]=null;
}
}
};
var but6=document.getElementById("but6");
eventUtil.addevent(but6,"click",aaa);
//but6.attachEvent("onclick",aaa);
</script>
</body>
</html>
2016-03-22
//IE
var but5=document.getElementById("but5");
but5.attachEvent("onclick",aaa);
//but5.detachEvent("onclick",aaa);
//跨瀏覽器
這段代碼需要注釋掉。按鈕6就能用,在“//跨瀏覽器”代碼中調(diào)用按鈕5,按鈕5就能用。
2016-03-22
而且第5個按鈕僅僅是在IE上才支持的,其他瀏覽器是不支持的。
2016-03-22
//DOM2級
var but4=document.getElementById("but4");
but4.addEventListener("click",aaa,false);
//but4.removeEventListener("click",aaa,false);
//IE
var but5=document.getElementById("but5");
but5.attachEvent("onclick",aaa);
//but5.detachEvent("onclick",aaa);
把這些都注釋掉。。。因為前面寫的這些是會對后面的能力選擇造成錯誤的。