html结构上(非视觉上)嵌套关系的元素,会存在事件捕获的功能,即同一事件,自父元素冒捕获至子元素(事件源)(自顶向下)
IE 没有捕获事件
和冒泡事件顺序相反 冒泡案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.red{
width: 300px;
height: 300px;
background-color: red;
}
.yellow{
width: 200px;
height: 200px;
margin-left: 300px;
background-color: yellow;
}
.green{
width: 100px;
height: 100px;
margin-left: 200px;
background-color: green;
}
</style>
</head>
<body>
<div class="red">
<div class="yellow">
<div class="green"></div>
</div>
</div>
<script>
var red = document.getElementsByClassName('red')[0],
yellow = document.getElementsByClassName('yellow')[0],
green = document.getElementsByClassName('green')[0];
red.addEventListener('click',function(){
console.log('red')
},true)
yellow.addEventListener('click',function(){
console.log('yellow')
},true)
green.addEventListener('click',function(){
console.log('green')
},true)
</script>
</body>
</html>
点击red
red事件执行
点击yellow
red 事件捕获 执行
yellow 事件执行
点击green
red 事件捕获 执行
yellow 事件捕获 执行
green 事件执行