JS事件处理(removeEventListener addEventListener)

时间:2021-01-09 19:29:55

<div>detachEvent</div>

1     window.onload = function(){
2       var oDiv = document.getElementById("contentDiv");
3       var func = function(){
4         alert(oDiv.innerHTML);
5       }
6       var funcAlso = function(){
7         alert("also " + oDiv.innerHTML);
8       }
9       //IE
10       if(oDiv.attachEvent){
11         oDiv.attachEvent("onclick",func);
12         oDiv.attachEvent("onclick",funcAlso);
13         //oDiv.detachEvent("onclick",func);
14       } else if(oDiv.addEventListener){
15         //FireFox
16         oDiv.addEventListener("click",func,true);
17         oDiv.addEventListener("click",funcAlso,true);
18         //oDiv.removeEventListener("click",func,true);
19       }
20      
21     }