当前位置:网站首页>DOM - Event Delegate
DOM - Event Delegate
2022-08-02 16:36:00 【z_Xiao Zhang classmate】
Event delegation, also known as event delegation, is a design idea in web design, that is, binding the events of one or a group of elements (target elements) to the parent or ancestor elementsuperior.It is the outer element that actually binds the event, not the target element.
When the event responds to the target element, it will trigger the binding event of its outer element through the event bubbling mechanism, and then execute the function on the outer element.
Event proxy is mainly used to solve the problem of static event binding; to solve the problem of the same business but higher performance consumption.
Benefits of event delegation:
(1) Reduce performance consumption, reduce the memory required for the entire page, and improve overall performance (reduce redundancy and save resources)
(2) Dynamic binding can be achieved to reduce repetitive work
Cons:
(1) Some events (without bubbling feature) cannot be bound with event proxy, such as focus, blur
(2) For events such as mousemove and mouseout, although there is event bubbling, it needs to continuously calculate the position and positioning, which has high performance consumption and is not suitable for event proxy
(3) If all events are delegated by events, event misjudgment may occur, that is, events that should not be triggered are bound to events
Events suitable for event delegation:
click, mousedown, mouseup, keydown, keyup, keypress
For example: do not use event proxy, set the click event to box2 in the normal way, and print out the content when the div of box2 is clicked.
box1box2box3box4
The above design has two disadvantages:
1. Static event binding: if an element is added dynamically, the added element does not have this event
2. The performance consumption is higher but the business is the same
But the same requirement we can use event delegate to solve:
box1box2box3box4
By means of event proxy, the business of box2 is directly bound to its parent element box1 for execution, which not only improves performance, but also executes this event even for new elements added later.
边栏推荐
猜你喜欢
随机推荐
smart rtmpd web 接口说明
【无标题】
小知识点系列:StringUtil.isEmpty()与StringUtil.isBlank()的区别
超简单了解三次握手与四次挥手
【时间序列模型】AR模型(原理剖析+MATLAB代码)
一线大厂研发流程(转载自鱼皮)
加点字符就能让qq昵称很酷的神奇代码?
Spark的概念、特点、应用场景
Scala的模式匹配与样例类
在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
数据库性能优化的误区!
WebRTC 中有关 Media Stream & Track & Channel 之间的关系
WeTest----如何查看Wetest生成测试报告?
golang时间-时间戳的获取-转换-计算
LAMP环境 源码编译安装(Apache 2.4.52 +mysql 8.0.28+php 8.1.3)
UINIX 高级环境编程杂项之限制
【软件测试】用例篇
个人成长系列:业务、技术学习书单
WEB自动化之键盘、鼠标操作
filebeat的配置