<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
width: 500px;
height: 500px;
background-color: red;
}
.son{
width: 200px;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<script>
const father = document.querySelector('.father')
const son = document.querySelector('.son')
//不加true,默认为false,是冒泡事件(小到大)
//加true,是捕获事件(大到小)
document.addEventListener('click',function(){
alert('我是爷爷')
})
father.addEventListener('click',function(){
alert('我是爸爸')
})
son.addEventListener('click',function(e){
alert('我是儿子')
//阻止流动传播(方法) 事件对象.stopPropagation()
e.stopPropagation() //只弹出我是儿子
})
</script>
</body>
</html>
03-事件流
于 2023-05-29 21:53:17 首次发布