当前位置:网站首页>DOM 事件及事件委托
DOM 事件及事件委托
2022-08-01 23:48:00 【前端码农小王】
DOM 事件及事件委托
DOM 事件模型
某一个元素有父元素,它的父元素还有父元素,它们都有监听事件,那么按什么顺序触发呢?
<div class=爷爷><div class=爸爸><div class=儿子>文字</div></div>
</div>
2002年,W3C发布标准
首先从外向内找监听函数,然后从内向外找监听函数
- 从外向内:事件捕获(Capture)
- 从内向外:事件冒泡(Bubble)
代码实现
开发者可以选择让自己的监听函数在捕获过程中执行,或在冒泡中执行
el.addEventListener('click', fn, bool)
第三个参数有3个值:
1.不传该参数,默认为false,该函数在冒泡过程中被调用
2.falsy值,该函数在冒泡过程中被调用 (6个falsy值:0,NaN,‘’,null,undefined,document.all)
3.true,该函数在捕获过程中被调用
默认为false,说明还是更倾向于事件冒泡
target v.s. currentTarget
区别
- e.target - 用户操作的元素
- e.currentTarget - 程序员监听的元素
- this 是 e.currentTarget
举例
div > span{文字},用户点击文字,e.target 就是 span,e.currentTarget 就是 div
取消冒泡
捕获不可取消,但冒泡可以e.stopPropagation() 可中断冒泡,浏览器不再向上走,一般用于封装某些独立的组件
阻止默认事件
MDN 搜索 scroll event,看到 Bubbles 和 Cancelable
Bubbles 的意思是该事件是否冒泡,所有冒泡都可取消
Cancelable 的意思是开发者是否可以阻止默认事件
Cancelable 与冒泡无关
事件委托
事件委托又称事件代理(delegate)
把原本需要绑定在子元素的响应事件绑定到(委托给)祖先节点,让祖先节点担当事件监听的职务
事件代理的原理是DOM元素的事件冒泡,因为在事件传播过程中,逐层冒泡总能被祖先节点捕获
优点:
1.省监听数(内存)* 假设父元素div下有100个span子元素,那么给100个span子元素都添加监听器所需要的资源远大于只给div加1个监听器
2.可以监听动态元素* 即使该元素还没有被创立,那么该元素创立后能立即响应监听,因为监听器在其父元素身上,即新增子元素无需再绑定监听
举例:
一个班级的同学的新学期教材到了,一种方法就是他们一个个去领,另一种方法就是把这件事情委托给班长,让一个人出去拿好所有教材,然后再根据分发给每个同学;即使有同学后面才来,也能立刻到班长那里领到教材
JS 支持事件吗
支持,也不支持。本节课讲的 DOM 事件不属于 JS 的功能,属于浏览器提供的 DOM 的功能,JS 只是调用了 DOM 提供的 addEventListener 而已
封装事件委托
jQuery已经封装了事件委托
.on( events [, selector ] [, data ], handler )
边栏推荐
- 20220725 Information update
- Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
- FAST-LIO2代码解析(二)
- @Transactional 注解使用详解
- recursion: method calls itself
- numpy.around
- 一款简洁的文件传输工具
- Enterprise firewall management, what firewall management tools are there?
- chrome copies the base64 data of an image
- CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
猜你喜欢
![[Camp Experience Post] 2022 Cybersecurity Summer Camp](/img/1e/716bafc679dc67d3d54bcc21a3b670.png)
[Camp Experience Post] 2022 Cybersecurity Summer Camp

Flink Yarn Per Job - 提交流程一

Access the selected node in the console

2022 6th Strong Net Cup Part WP

Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions

cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed

1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?

nodejs--process

如何进行数据库备份

在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
随机推荐
npm npm
Work for 5 years, test case design is bad?To look at the big case design summary
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
Chapter 11 Working with Dates and Times
numpy.around
6134. Find the closest node to the given two nodes - force double hundred code
cmd command
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
Architecture basic concept and nature of architecture
Flink学习第四天——完成第一个Flink 流批一体案例
在MySQL中使用MD5加密【入门体验】
如何更好的理解的和做好工作?
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
Additional Features for Scripting
获取小猪民宿(短租)数据
6132. 使数组中所有元素都等于零-快速排序法
高效工作文档产出归类
solidity
如何进行数据库备份