当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
华为Mux VLAN 二层流量隔离
smart_rtmpd 轻松突破 C100K 测试
golang中使用泛型
假的服务器日志(给history内容增加ip、用户等内容)
数据库性能优化的误区!
mongodb连接本地服务失败的问题
makefile——library
虚拟机使用的是此版本 VMware Workstation 不支持的硬件版本。模块“Upgrade”启动失败。未能启动虚拟机。
【数据读写】csv文件与xls/xlsx文件
网络运维系列:Ubnt ER-X初始化和开启硬件NAT
8.0以上MySQL的常见错误
Oauth2.0 Supplement
VLAN原理
nvm管理node版本 nodenpm不是内部或外部命令,也不是可运行的程序
APP版本更新通知流程测试要点
webrtc 中怎么根据 SDP 创建或关联底层的 socket 对象?
Golang基础教程
支付系列文章:PCI合规能力建设
Golang学习(三十五) go 连接redis
一、QT界面开发 --QT安装