当前位置:网站首页>事件捕获方式和冒泡方式—它们的区别是什么?
事件捕获方式和冒泡方式—它们的区别是什么?
2022-07-27 05:24:00 【THER1881】
事件冒泡、捕获:事件冒泡和事件捕获分别由微软和网景公司提出,这两个概念都是为了解决页面中事件流(事件发生顺序)的问题。
1、事件冒泡
微软提出了名为事件冒泡的事件流。结构上(非视觉上)嵌套关系的元素,会存在冒泡的功能,即同一事件,自子元素冒泡向父元素。(自底向上)
那么接下来我们来验证一下:
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 100px;
background-color: red;
text-align: center;
line-height: 100px;
cursor: pointer;
}
</style>
<body>
<div>点击</div>
<script>
let body = document.querySelector('body')
let div = document.querySelector('div')
body.addEventListener('click',function(){
console.log('body')
})
div.addEventListener('click',function(){
console.log('1')
})
</script>
</body>

2、事件捕获
结构上(非视觉上)嵌套关系的元素,会存在事件捕获的功能,即同一事件,自父元素捕获至子元素(事件源元素)。(自顶向下)(ie没有捕获事件)
那么接下来我们来验证一下:
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 100px;
background-color: red;
text-align: center;
line-height: 100px;
cursor: pointer;
}
</style>
<body>
<div>点击</div>
<script>
let body = document.querySelector('body')
let div = document.querySelector('div')
body.addEventListener('click',function(){
console.log('body')
},ture)
div.addEventListener('click',function(){
console.log('1')
},ture)
</script>
</body>

区别
事件冒泡:一个由里到外,冒泡,向上层延申。
事件捕获:一个由外到里,前往捕获,形成向下趋势。
边栏推荐
- Shell -- conditional statements (if statements, case statements)
- ESXI虚拟机启动,模块“MonitorLoop”打开电源失败
- Lamp -- source code compilation and installation
- ArcGIS for JS API entry series
- Sunflower: don't worry when you encounter computer vulnerabilities, understand clearly and then judge sunflower: don't worry when you encounter computer vulnerabilities, understand clearly and then ju
- Shell脚本一键配置LAMP
- Shell script delete automatically clean up files that exceed the size
- Boostrap
- Packaging of logging logs
- GoLand writes Go program
猜你喜欢

win10 添加虚拟网卡,配置op路由

账号管理与权限

According to SQL, you must know and learn SQL (MySQL)

1. CentOS 7 安装 redis

Publish a building segmentation database with a resolution of 0.22m

DNS domain name resolution service

Pruning - quantification - turn to onnx Chinese series tutorials

Geonode GeoServer win10 installation tutorial (personal test)

Lamp -- source code compilation and installation

Inventory of the world's six most technologically competent smart contract audit companies in 2022
随机推荐
关于过快S验证码的一些问题
PXE高效批量网络装机
GoLand 编写go程序
Some applications of std:: bind and std:: function
Brief introduction of chip, memory and its key indicators I
Shell语句判断练习题
When a subclass calls the constructor of its parent class
多模态数据库 | 星环科技多模数据库ArgoDB“一库多用“,构建高性能湖仓集一体平台
Concept and principle of DHCP
How to avoid loopholes? Sunflower remote explains the safe use methods in different scenarios
Redis fast learning
Three methods to judge whether it is palindrome structure
Tips - completely delete the files on the USB flash drive
logging日志的封装
Log in to Alibaba cloud server with a key
一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告
FTP服务简介与配置
ArcGIS for JS API (2) get the ID set of element services
3D打印品牌的康复骨科支具有何特别之处?
GoLand writes Go program