当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
webrtc 中怎么根据 SDP 创建或关联底层的 socket 对象?
在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
关于导出聊天记录这件事……
抽象类和接口 基本知识点复习
test3
makefile——pattern rule
Spark的概念、特点、应用场景
JS中的数组方法和循环
MATLAB中dist与pdist、pdist2的区别与联系
Homebrew的简单介绍
Mysql开启日志并按天进行分割
SQL在MySQL中是如何执行的
【无标题】
小知识系列:Fork之后如何与原仓库分支同步
mongodb连接本地服务失败的问题
【网络参考模型】
网络运维系列:二级域名启用与配置
Template series-union set
假的服务器日志(给history内容增加ip、用户等内容)
【软件测试】用例篇