DOM Event delegation

DOM event delegation is a mechanism of responding to ui-events via a single common parent rather than each child, through the magic of event "bubbling" (aka event propagation).

When an event is triggered on an element, the
following occurs
:

The event is dispatched to its target EventTarget and
any event listeners found there are triggered.Bubbling events will then trigger any additional event listeners found by
following the EventTarget‘s
parent chain upward, checking for any event listeners registered on each successive EventTarget. This upward propagation
will continue up to and including the Document.

Event bubbling provides the foundation for event delegation in browsers. Now you can bind an event handler to a single parent element, and that handler will get executed whenever the event occurs on
any of its child nodes(and any of their children in turn). This is event delegation. Here‘s an example of it in
practice:

<ul onclick="alert(event.type + ‘!‘)">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

With that example if you were to click on any of the child <li> nodes,
you would see an alert of "click!",
even though there is no click handler bound to the <li> you
clicked on. If we bound onclick="..." to
each <li> you
would get the same effect.

So what‘s the benefit?

Imagine you now have a need to dynamically add new <li> items
to the above list via DOM manipulation:

var newLi = document.createElement(‘li‘);
newLi.innerHTML = ‘Four‘;
myUL.appendChild(newLi);

Without using event delegation you would have to "rebind" the "onclick" event
handler to the new <li>element,
in order for it to act the same way as its siblings. With event delegation you don‘t need to do anything. Just add the
new <li> to
the list and you‘re done.

This is absolutely fantastic for web apps with event handlers bound to many elements, where new elements are dynamically created and/or removed in the DOM. With event delegation the number of event bindings can be drastically decreased by moving them to a common
parent element, and code that dynamically creates new elements on the fly can be decoupled from the logic of binding their event handlers.

Another benefit to event delegation is that the total memory footprint used by event listeners goes down (since the number of event bindings go down). It may not make much of a difference to small pages that unload often (i.e. user‘s navigate to different pages
often). But for long-lived applications it can be significant. There are some really difficult-to-track-down situations when elements removed from the DOM still claim memory (i.e. they leak), and often this leaked memory is tied to an event binding. With event
delegation you‘re free to destroy child elements without risk of forgetting to "unbind" their event listeners (since the listener is on the ancestor). These types of memory leaks can then be contained (if not eliminated, which is freaking hard to do sometimes.
IE I‘m looking at you).

Here are some better concrete code examples of event delegation:

DOM Event delegation

时间: 2024-08-28 08:38:40

DOM Event delegation的相关文章

JavaScript Interview Questions: Event Delegation and This

David Posin helps you land that next programming position by understanding important JavaScript fundamentals. JavaScript is a fun, powerful, and important language with a low barrier of entry. People from all kinds of backgrounds and careers find the

JavaScript------事件委托(event delegation)

简单的说,事件委托(event delegation)是在DOM上层(也就是在触发事件的元素的父元素上)定义事件的处理程序,而不是定义在触发事件的元素本身上. 首先我们来举这样一个例子:我有N个li元素,我想让点击过的li元素背景颜色都变为黄色.非常简单的一个例子是吧? 我们可以通过如下代码实现: var oBox = document.getElementById('box');var aLi = oBox.children;for(var i=0;i<aLi.length;i++){ add

[DOM Event Learning] Section 1 DOM Event 处理器绑定的几种方法

[DOM Event Learning] Section 1 DOM Event处理器绑定的几种方法 网页中经常需要处理各种事件,通常的做法是绑定listener对事件进行监听,当事件发生后进行一些特定处理. 监听事件的几种方法如下文. 第一种,写在页面标签里面 <button onclick="alert('Hello')">Say hello</button> 上面这行代码,将按钮点击后的弹窗操作在标签声明的时候就绑定了. 这是一种糟糕的方法,原因如下: 1

事件代理 event delegation

JS - 事件代理 如果你想给网页添加点JavaScript的交互性,也许你已经听过JavaScript的事件代理(event delegation),并且觉得这是那些发烧友级别的JavaScript程序员才会关心的什么费解的设计模式之一.事实上,如果你已经知道怎么添加JavaScript的事件处理器(event handler),实现事件代理也是件轻而易举的事情. JavaScript事件是所有网页互动性的根基(我指的是真正的互动性,而不仅是那些CSS下拉菜单).在传统的事件处理中,你按照需要

HTML DOM Event对象

我们通常把HTML DOM Event对象叫做Event事件 事件驱动模型 事件源:(触发事件的元素)事件源对象是指event对象 其封装了与事件相关的详细信息. 当事件发生时,只能在事件函数内部访问的对象 处理函数结束后会自动销毁 事件流:当页面元素触发事件的时候, 该元素的容器以及整个页面都会按照特定的顺序响应该元素的触发事件 事件传播的顺序叫做事件流. 事件分类:鼠标事件,键盘事件,表单事件,页面事件 我这里主要列举鼠标事件的: 1.鼠标事件: var btnNum = event.but

HTML DOM event 事件 (丫头, 好想你了)

HTML DOM event 事件, 用用就知道了 function isKeyPressed(eventssdasd)     // 这个地方其实无关紧要,就是定义一个名称而已, 不过为了好看, 最好还是写成 event{  if (event.altKey==1)                        // event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等,此处为检测 alt 键是否按下    {  // 此处的 event 必须写正确, 你可以

[DOM Event Learning] Section 2 概念梳理 什么是事件 DOM Event

[DOM Event Learning] Section 2 概念梳理 什么是事件 DOM Event 事件 事件(Event)是用来通知代码,一些有趣的事情发生了. 每一个Event都会被一个Event对象所表示,这个对象可能还会有一些自定义的字段或者方法,来获取发生什么事情的更多信息. Event对象实现了Event接口(https://developer.mozilla.org/en-US/docs/Web/API/Event). 事件可以是任何事情,从最基本的用户交互,到renderin

javascript 事件委托 event delegation

事件委托 event delegation 一.概念: 假设我们有很多个子元素,每个元素被点击时都会触发相应事件,普通的做法是给每个子元素添加一个事件监听. 而,事件委托则是给它们的父元素添加一个事件监听器,子元素上没有任何事件监听.当子元素被点击时,这个点击事件冒泡到父元素上,然后父元素上绑定的事件监听来分析和处理这是哪个子元素被点击了. 二.例子1:一个ul及几个li: <ul id="parent-list" style="border:1px solid bla

[DOM Event Learning] Section 4 事件分发和DOM事件流

[DOM Event Learning] Section 4 事件分发和DOM事件流 事件分发机制: event dispatch mechanism. 事件流(event flow)描述了事件对象在数据结构中是如何传播的. 传播路径 事件对象(event objects)被分发给事件目标(event target),在分发开始的时候,在实现中必须先确定事件对象的传播路径. 这个传播路径必须是一个有序的list,其中包含了事件对象必须通过的事件目标. 对于DOM的实现来说,这个传播路径必须反映这