javascript事件处理------hml事件处理程序、dom0事件处理程序、dom2事件处理程序

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

</head>
<body>
     <h1 id="d1">ddd</h1>
     <h2 id="d2">ddd</h2>
     <h3 onclick="alert(this.tagName+'\n'+this.id);" id="d3">ddd</h3>
     <h4 id="d4">ddd</h4>
            <script type="text/javascript" defer="defer">
                  var od1 = document.getElementById("d1");
                  od1.addEventListener("click",function(){alert(this.tagName+"\n"+this.id);},false); 
                  od1.addEventListener("click",function(){alert(this.tagName+"\n"+this.innerText);},false); //firefox不支持innerText                  
                  var od2 = document.getElementById("d2");
                  od2.onclick=function(){
                          alert(this.tagName+"\n"+this.id);    
                  }
                  var od4 = document.getElementById("d4");
                  od4.addEventListener("click",handler,false);
                  function handler(){
                      alert(this.tagName + "\r" +this.id);    
                  }
                  //od4.removeEventListener("click",handler,false);
    </script>
</body>


</html>
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

</head>
<body>
ie比较怪异
<h5 id="d5">ddd</h5> <script type="text/javascript" defer="defer"> var od5 = document.getElementById("d5"); od5.attachEvent("onclick",function(){ alert("Clicked"); }); </script> </body> </html>
 
 

 

 

你可能感兴趣的:(JavaScript)