事件顺序模型的不同

2016-02-22    编辑:safeman     点击(

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>New document</title>
<style type="text/css">
<!--
div   { border:1px solid #000;}
div#div1 { padding:40px;width:260px;}
div#div2 { padding:40px;background-color:#aaaaaa;}
-->
</style>
</head>
<body>
<div id="div1">
 <div id="div2">
  click me
 </div>
</div>
</body>
</html>

<script language="JavaScript" type="text/javascript">
<!--
function showDiv1(){
 alert("div1");
}

function showDiv2(){
 alert("div2");
}
var div1=document.getElementById("div1");
var div2=document.getElementById("div2");


//根据浏览器采用不同的事件绑定语法
if(window.attachEvent){
 div1.attachEvent("onclick",showDiv1);
 div2.attachEvent("onclick",showDiv2);
}else{
 div1.addEventListener("click",showDiv1,true);
 div2.addEventListener("click",showDiv2,true);
}
//-->
</script>