<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* .father {
position: relative;
width: 500px;
height: 500px;
pink;
margin: 0 auto;
} */
.father {
overflow: hidden;
width: 300px;
height: 300px;
margin: 100px auto;
background-color: pink;
text-align: center;
}
/* .son {
position: absolute;
left: 100px;
top: 100px;
width: 300px;
height: 300px;
purple;
} */
.son {
width: 200px;
height: 200px;
margin: 50px;
background-color: purple;
line-height: 200px;
color: #fff;
}
</style>
</head>
<body>
<div class="father">
<div class="son">son盒子</div>
</div>
<script>
// true 是 捕获阶段 从上往下 body-》father-》son
// var son = document.querySelector(‘.son‘);
// son.addEventListener(‘click‘, function() {
// alert(‘son‘);
// }, true);
// var father = document.querySelector(‘.father‘);
// father.addEventListener(‘click‘, function() {
// alert(‘father‘);
// }, true);
// 默认是冒泡阶段 从下往上 son-》》father-》body
var son = document.querySelector(‘.son‘);
var father = document.querySelector(‘.father‘);
father.addEventListener(‘click‘, function() {
alert(‘father‘);
});
son.addEventListener(‘click‘, function() {
alert(‘son‘);
});
</script>
</body>
</html>