jQuery in Action

94
CHAPTER 4
Events are where it happens!
Note that even though the handlers fire in the order in which they were estab-
lished, this order isn't guaranteed by the standard! Testers of this code never observed
an order other than the order of establishment, but it would be foolish to write
code that relies on this order. Always be aware that multiple handlers established
on an element may fire in random order.
Now, what's up with that
useCapture
parameter?
Event propagation
We saw earlier that, with the Basic Event Model, once an event was triggered on
an element the event propagated from the target element upwards in the
DOM
tree to all the target's ancestors. The advanced Level 2 Model also provides this
bubbling phase but ups the ante with an additional phase: capture phase.
Under the
DOM
Level 2 Event Model, when an event is triggered, the event
first propagates from the root of the
DOM
tree down to the target element and
then propagates again from the target element up to the
DOM
root. The former
phase (root to target) is called capture phase, and the latter (target to root) is called
bubble phase.
When a function is established as an event handler, it can be flagged as a cap-
ture handler in which case it will be triggered during capture phase, or as a bub-
ble handler to be triggered during bubble phase. As you might have guessed by
this time, the
useCapture
parameter to
addEventListener()
identifies which type
of handler is established. A value of
false
for this parameter establishes a bubble
handler, whereas a value of
true
registers a capture handler.
Figure 4.3 Clicking the image once demonstrates that all three handlers established
for the click event are triggered.


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.