addEventListener和attachEvent的区别

时间:2022-06-29 19:37:07
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1>Vue demo</h1>
 
</div>
</div>
<script>
var h1=document.getElementsByTagName("h1")[0];
 
if(h1.addEventListener){
h1.addEventListener("click",fun1);
}else if(h1.attachEvent){
 
h1.attachEvent("onclick",fun2);
//h1.onclick=function(){alert(2)}
};
 
function fun1(){alert("1")};
function fun2(){alert("2")};
 
</script>
 
 
注意:1、attachEvent监听事件中,加on
2、attachEvent用于ie8以及更低的ie版本