2014 Sinsing decoding JavaScript's dom bubble and capture

Source: Internet
Author: User

The previous blog mentions the three parameters of the JavaScript event binding function. The first one is an event. The second one is a function. The third one is a Boolean variable. It is used to specify the order in which events are passed, divided into two ways of bubbling and capturing, and then we will uncover its magical veil and thoroughly interpret it.

First look at the following code, for example:

So for the above HTML file, if we click on the word "Sinsing", then which pop-up window first? Assuming that the reader copies the above code, the suffix is named an HTML file, then it will be found that the popup window "tag p is triggered". What is this mechanism when we are certain that the tag div is triggered?

This is because the third parameter is in effect, the third parameter is false by default, that is, in the way of bubbling, what is bubbling? Let's say we've learned the algorithm. Will find a bubble sort, very easy, is that the bubbles will float from the bottom up. This means that we respond to small labels first. In response to a large label, for example, the P tag is a sub-element of the DIV tag, so. This first responds to the event that the P tag binds. Then respond to the click events of the div tag bindings.

Suppose you change the third parameter to True. The effect is exactly the opposite, first responding to the parent tag's event. The event that responds to the child label again. Let's say these things, it seems that this blog post is dine, then we will introduce some things. Let's talk about RemoveEventListener this function. It is used to unbind a callback function that binds our events and events.

Just this addeventlistener and RemoveEventListener for browser compatibility is not so good, or IE series in mischief, IE8 under the version number is not supported, However, IE8 supports the use of Element.attachevent (event. function) to bind and unbind with Element.detachevent (event,function).

for browser compatibility. It's a really annoying problem. Especially in China such as the use of IE is very large countries, alas, can only slowly endure torture.

2014 Sinsing decoding JavaScript's dom bubble and capture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.