1.捕获:a->b,
2.冒泡:b->a
3.捕获优先于冒泡
一.捕获
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
body{
color: #fff;
font-size: 1.5rem;
}
.a{
width:500px;
height: 500px;
background-color: red;
position: relative;
}
.b{
width: 300px;
height: 300px;
position: absolute;
background-color:orange;
}
</style>
</head>
<body>
<div class="a">
a
<div class="b">
b
</div>
<div class="c">
c
</div>
</div>
<script>
var a = document.getElementsByClassName('a')[0];
var b = document.getElementsByClassName('b')[0];
var c = document.getElementsByClassName('c')[0];
a.addEventListener("click",function(e){
// e.stopImmediatePropagation();
console.log("a");
},true);
b.addEventListener("click",function(e){
// e.stopImmediatePropagation();
console.log("b");
},true);
</script>
</body>
</html>
二.冒泡
<script>
var a = document.getElementsByClassName('a')[0];
var b = document.getElementsByClassName('b')[0];
var c = document.getElementsByClassName('c')[0];
a.addEventListener("click",function(e){
// e.stopImmediatePropagation();
console.log("a");
},false);
b.addEventListener("click",function(e){
// e.stopImmediatePropagation();
console.log("b");
},false);
</script>
3.停止捕获
var a = document.getElementsByClassName('a')[0];
var b = document.getElementsByClassName('b')[0];
var c = document.getElementsByClassName('c')[0];
a.addEventListener("click",function(e){
e.stopImmediatePropagation();
console.log("a");
},true);
b.addEventListener("click",function(e){
// e.stopImmediatePropagation();
console.log("b");
},true);
2.停止冒泡
var a = document.getElementsByClassName('a')[0];
var b = document.getElementsByClassName('b')[0];
var c = document.getElementsByClassName('c')[0];
a.addEventListener("click",function(e){
// e.stopImmediatePropagation();
console.log("a");
},false);
b.addEventListener("click",function(e){
e.stopImmediatePropagation();
console.log("b");
},false);