DOM Event delegation,domdelegation

來源:互聯網
上載者:User

DOM Event delegation,domdelegation

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:

  • 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 and blur events (which do not bubble)

在W3C手冊中的與DOM操作用中window與event都是對象,為何有時看到別人用windowevent這樣用?

window.event 是規範寫法 window 是頂層對象,event 是 window 的一個屬性,類型也是對象。
 
javascript event對象的具體功可以是什

event對象只在事件發生的過程中才有效(比如滑鼠點擊,鍵盤按下等)。
event對象用以表示事件的狀態,
例如
觸發event對象的元素(event.srcElement)、
滑鼠的位置(event.clientX、event.clientY)、
按下的鍵(event.keyCode)等等。

event對象的屬性包括:
altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, keyCode, offsetX, offsetY, propertyName, returnValue, screenX, screenY, shiftKey, srcElement, srcFilter, toElement, type, x, y (可以詳細查看參考書)

使用event的方法:

定義
var evt = window.event;
FF中不能通過該方法得到event對象,可以通過傳值的方法:
element.onclick = function(e){
var evt = window.event || e;
...
}

用event獲得點擊時滑鼠的座標
element.onclick = function(e){
var evt = window.event || e;
var cursorPOS = {
x : evt.clientX,
y : clientY
}
}

用event對象指定當按下斷行符號鍵時,彈出警告框
element.onkeydown = function(e){
var evt = window.event || e;
if(evt.keyCode==13){
alert('按下了斷行符號!');
}
}

用event獲得滑鼠點擊的DOM對象
document.onclick=function(e){
var evt = window.event || e;
var _target = evt.srcElement || evt.target;
alert(_target.tagName)
}

更多的使用,請參考教材。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.