Event order

本文详细解释了Web开发中事件捕获与冒泡的概念,通过实例展示了如何在DOM模型中注册事件处理函数,并在不同阶段执行。同时讨论了传统模型的兼容性,如何关闭事件捕获和冒泡以优化性能,以及如何利用currentTarget属性解决目标元素识别问题。

Two models

W3C model

The web developer, can choose whether to register an event handler in the capturing or in the bubbling phase.

This is done through the addEventListener() method. If its last argument is true the event handler is set for the capturing phase, if it is false the event handler is set for the bubbling phase.

该函数最后一个参数设为真,把事件句柄注册在捕捉阶段,如果设为假,则把事件句柄注册在冒泡阶段。

Example

element1.addEventListener('click',doSomething2,true)
element2.addEventListener('click',doSomething,false)

If the user clicks on element2 the following happens:

  1. The click event starts in the capturing phase. The event looks if any ancestor element of element2 has a onclick event handler for the capturing phase.
  2. The event finds one on element1. doSomething2() is executed.
  3. The event travels down to the target itself, no more event handlers for the capturing phase are found. The event moves to its bubbling phase and executes doSomething(), which is registered to element2 for the bubbling phase.
  4. The event travels upwards again and checks if any ancestor element of the target has an event handler for the bubbling phase. This is not the case, so nothing happens.

element1.addEventListener('click',doSomething2,false)
element2.addEventListener('click',doSomething,false)

Now if the user clicks on element2 the following happens:

  1. The click event starts in the capturing phase. The event looks if any ancestor element of element2 has a onclick event handler for the capturing phase and doesn’t find any.
  2. The event travels down to the target itself. The event moves to its bubbling phase and executes doSomething(), which is registered to element2 for the bubbling phase.
  3. The event travels upwards again and checks if any ancestor element of the target has an event handler for the bubbling phase.
  4. The event finds one on element1. Now doSomething2() is executed.

Compatibility with traditional model

In the browsers that support the W3C DOM, a traditional event registration

element1.onclick = doSomething2;

is seen as a registration in the bubbling phase.

Turning it off

What you first need to understand is that event capturing or bubbling always happens, any event ends up on the document.

Usually you want to turn all capturing and bubbling off to keep functions from interfering with each other. Besides, if your document structure is very complex (lots of nested tables and such) you may save system resources by turning off bubbling.

当你的页面非常复杂时,你会想要关掉捕捉和冒泡功能,以防止互相干扰和节省系统资源。

The browser has to go through every single ancestor element of the event target to see if it has an event handler. Even if none are found, the search still takes time.

window.event.cancelBubble = true; // Microsoft model
e.stopPropagation(); // W3C model

 

For a complete cross-browser experience do

function doSomething(e)
{
  if (!e) var e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
}
Setting the  cancelBubble  property in browsers that don’t support it doesn’t hurt.

CurrentTarge

An event has a target or srcElement that contains a reference to the element the event happened on.

It is very important to understand that during the capturing and bubbling phases (if any) this target does not change.

element1.onclick = doSomething;
element2.onclick = doSomething;
If the user clicks on element2  doSomething()  is executed twice. But how do you know which HTML element is currently handling the event? target/srcElement  don’t give a clue, they always refer to element2 since it is the original source of the event.
To solve this problem W3C has added the  currentTarget  property. It contains a reference to the HTML element the event is currently being handled by.

Unfortunately the Microsoft model doesn’t contain a similar property. You can also use thethis keyword.

jQuery .bind() vs .on()

.bind()  Attach a handler to an event for the elements.

.on()  Attach an event handler function for one or more events to the selected elements.

When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound. After all handlers have executed, the event continues along the normal event propagation path.

文章地址:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值