DOM Event delegation
来源:互联网 发布:淘宝个人空间在哪里 编辑:程序博客网 时间:2024/05/18 09:52
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 theEventTarget
's parent chain upward, checking for any event listeners registered on each successive EventTarget. This upward propagation will continue up to and including theDocument
.
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:
- How JavaScript Event Delegation Works
- Event Delegation versus Event Handling
- jQuery.delegate is event delegation + selector specification
- jQuery.on uses event delegation when passed a selector as the 2nd parameter
- Event delegation without a JavaScript library
- Closures vs Event delegation: takes a look at the pros of not converting code to use event delegation
- Interesting approach PPK uncovered for delegating the
focus
andblur
events (which do not bubble)
- DOM Event delegation
- Event delegation
- Event delegation in JavaScript
- the advantages of event delegation
- How JavaScript Event Delegation Works
- How JavaScript Event Delegation Works
- JS事件委托(event delegation)
- Delegation
- js | event delegation 事件委托之双十一
- Dom event listener
- HTML DOM Event 对象
- DOM和Event
- HTML DOM Event Object
- HTML DOM onload Event
- HTML DOM Event 对象
- WebKit DOM Event 实现
- HTML DOM Event 对象
- WebKit DOM Event (一)
- 俞敏洪|马云|牛根|史玉柱|经典语录大全
- CXF实现webService服务(一)
- Android应用开发相关版本下载资源
- HDU 1098 Ignatius's puzzle(数论-其它)
- C++重载与重写
- DOM Event delegation
- hdu 2045 不容易系列之(3)—— LELE的RPG难题
- oc的switch中使用alloc,new语法报错解决方法
- SQL Server孤立用戶
- PM如何写好产品需求文档
- 10010 - Where's Waldorf?(找出单词的位置,可以在八个直线方向)
- 用netbeans和xdebug调试php的配置
- android_launcher的源码详细分析
- 面试中的SQL分析一