Event bubbling in Javascript

Source: Internet
Author: User

This is a basic article. It uses JavaScript to observe the event bubbling mechanism in the Dom and describes how to block default behavior and how to organize event bubbling.

1. The first example can be run in Firefox.

<Div id = "container1" onclick = "alert ('click container1');">
<Div id = "container2" onclick = "alert ('click container2');">
<A href = "http://www.google.com" target = "_ blank" onclick = "fn1 (event);"> Google </a>
<A href = "http://www.google.com" target = "_ blank" onclick = "FN2 (event);"> Google </a>
<A href = "http://www.google.com" target = "_ blank" onclick = "fn3 (event);"> Google </a>
<A href = "http://www.google.com" target = "_ blank" onclick = "FN4 (event);"> Google </a>
</Div>
</Div>

 

Function fn1 (event ){
Alert ('click Google ');
}

Function FN2 (event ){
Alert ('click Google ');
Event. preventdefault ();
}

Function fn3 (event ){
Alert ('click Google ');
Event. stoppropagation ();
}

Function FN4 (event ){
Alert ('click Google ');
Event. preventdefault ();
Event. stoppropagation ();
}

 

Click the first link, alert_google-> alert_container2-> alert_container1-> open_google_page

Click the second link, alert_google-> alert_container2-> alert_container1

Click the third link, alert_google-> open_google_page

Click the fourth link, alert_google

 

We can see that event bubbling starts from the HTML node where the event was initially triggered, and raises the same event of the parent node step by step.

In Firefox, we can use the preventdefault function to block default behaviors (for example, in this example, the default behavior of clicking a link is to open the link address)

Use the stoppropagation function to prevent event bubbles.

 

The implementation of the same process in IE is somewhat different. First, the event in IE is an attribute of the window object,

The second is to block the default behavior of the event or to prevent the event from bubbling. For details, see:

2. Observe the event bubbles in IE.

 

<Div id = "containerpolicie" onclick = "alert ('click container1');">
<Div id = "container2_ie" onclick = "alert ('click ininer2');">
<A href = "http://www.google.com" target = "_ blank" onclick = "fn1_ie ();"> Google </a> <
Href = "http://www.google.com" target = "_ blank" onclick = "fn2_ie ();"> Google </a>
<A href = "http://www.google.com" target = "_ blank" onclick = "fn3_ie ();"> Google </a> <
Href = "http://www.google.com" target = "_ blank" onclick = "fn4_ie ();"> Google </a>
</Div>
</Div>

 

Function fn1_ie (){
Alert ('click Google ');
}

Function fn2_ie (){
Alert ('click Google ');
Event. returnvalue = false;
}

Function fn3_ie (){
Alert ('click Google ');
Event. cancelbubble = true;
}

Function fn4_ie (){
Alert ('click Google ');
Event. returnvalue = false;
Event. cancelbubble = true;
}

 

Similarly:

Click the first link, alert_google-> alert_container2-> alert_container1-> open_google_page

Click the second link, alert_google-> alert_container2-> alert_container1

Click the third link, alert_google-> open_google_page

Click the fourth link, alert_google

 

Code download

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.