<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>冒泡和捕获</title>
<style>
*{margin: 0;padding: 0;}
body{width:100%;}
div{width:80%;height: 80%;padding: 10%;position: relative;}
#a{background: rgba(255,0,0,.6);}
#b{background: rgba(0,255,0,.6);}
#c{background: rgba(0,0,255,.6);}
p{ color:#333;font-size: 1.2em;text-align: center; }
</style>
</head>
<body>
<div id="a">
<p> this is a div</p>
<div id="b">
<p>this is b div</p>
<div id="c">
<p>this is c div</p>
</div>
</div>
</div>
<script type="text/javascript">
var a = document.getElementById(‘a‘),
b = document.getElementById(‘b‘),
c = document.getElementById(‘c‘);
function addEvent(obj,ev,fn){
if(obj.attachEvent){//针对IE浏览器
obj.attachEvent(‘on‘+ev,fn)
}else{//针对FF与chrome
obj.addEventListener(ev,fn,false)//当为true时采用事件捕获,当为false采用事件冒泡
//程序员可以自己选择绑定事件时采用事件捕获还是事件冒泡。
}
}
addEvent(a,‘click‘,function(){
alert(‘this is a div‘);
});
addEvent(b,‘click‘,function(){
alert(‘this is b div‘);
});
addEvent(c,‘click‘,function(){
alert(‘this is c div‘);
});
</script>
</body>
</html>
原文:http://www.cnblogs.com/demokuma/p/6809667.html