子级会向父级传递自己的事件,如果父级也有相同事件则响应
false事件冒泡,true事件捕获
<style>
.wrapper {
width: 150px;
height: 150px;
background-color: aqua;
}
.outer {
width: 100px;
height: 100px;
margin-left: 150px;
background-color: bisque;
}
.inner {
width: 75px;
height: 75px;
margin-left: 100px;
background-color: blueviolet;
}
</style>
<div class="wrapper">
<div class="outer">
<div class="inner"></div>
</div>
</div>
<script>
var wrapper = document.getElementsByClassName('wrapper')[0];
var outer = document.getElementsByClassName('outer')[0];
var inner = document.getElementsByClassName('inner')[0];
wrapper.addEventListener('click', function() {
console.log('wrapper-冒泡');
}, false);
outer.addEventListener('click', function() {
console.log('outer-冒泡');
}, false);
inner.addEventListener('click', function() {
console.log('inner-冒泡');
}, false);
wrapper.addEventListener('click', function() {
console.log('wrapper-捕获');
}, true);
outer.addEventListener('click', function() {
console.log('outer-捕获');
}, true);
inner.addEventListener('click', function() {
console.log('inner-捕获');
}, true);
</script>
新版chrome:
旧版 chrome 和新旧版 Firefox、Safari 都还是顺序执行:
oDiv.addEventListener('click', function(e) {
var e = e || window.event;
e.stopPropagation();
}, false);
oDiv.addEventListener('click', function(e) {
var e = e || window.event;
e.cancelBubble = true;
}, false)