DOM 事件流 事件冒泡
<!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>
相关推荐
北京老苏 2020-08-17
星星有所不知 2020-10-12
zuncle 2020-09-28
huaoa 2020-09-14
luvhl 2020-08-17
Kakoola 2020-07-29
drdrsky 2020-07-29
书虫媛 2020-07-08
liaoxuewu 2020-07-08
SIMONDOMAIN 2020-07-08
爱读书的旅行者 2020-07-07
tianzyc 2020-07-04
Vue和React是数据驱动视图,如何有效控制DOM操作?能不能把计算,更多的转移为js计算?因为js执行速度很快。patch函数-->patch,对比tag,对比tag与key,对比children
Lophole 2020-07-04
Lophole 2020-06-28
liaoxuewu 2020-06-26
ApeLife技术 2020-06-26
北京老苏 2020-06-25
Lophole 2020-06-14