18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

All about Events<br />

Some have also called this the top-down event model because it works from the top of the DOM hierarchy<br />

to the bottom (see Figure 9-4).<br />

1<br />

document<br />

Figure 9-4<br />

2<br />

div<br />

DOM event flow<br />

The DOM supports both event capturing and event bubbling, but event capturing occurs first. Both<br />

event flows hit all the objects in the DOM, beginning and ending with the document object (most<br />

standards-compliant browsers continue capturing/bubbling up to the window object).<br />

Consider once again the simple example shown earlier. When the element is clicked in a DOMcompliant<br />

browser, the event flow proceeds as shown in Figure 9-5.<br />

1<br />

window 8<br />

2<br />

document<br />

7<br />

bubbling phase<br />

capturing phase<br />

3<br />

body<br />

6<br />

4<br />

div 5<br />

Figure 9-5<br />

Note that because the target of the event (the element) is the most specific element (and therefore,<br />

deepest in the DOM tree), it actually receives the event two times in a row, once in the capturing<br />

phase and once in the bubbling phase.<br />

265

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!