Javascript event bubbling Mechanism

Source: Internet
Author: User

I learned an event bubbling mechanism today and thought it was a bit problematic. I tested it myself and shared the results with you, which helps you better understand the event bubbling mechanism.


For example:

<Html>

<Head>

<Script>

Function f1 (){

Alert ('f1 ');

}

Function f2 (){

Alert ('F2 ');

}

Function f3 (){

Alert ('f3 ');

}

</Script>

</Head>

<Body>

<Div style = "background-color: red; width: 200px; height: 200px;">

<Div style = "background-color: blue; width: 100px; height: 100px;">

<A onclick = "f1 ();"> Hello </a>

</Div>

</Div>

</Body>

</Html>


After you click "hello", f1 will pop up, and f2 f3 will pop up. Then, when the sub-tag generates an event, it will be passed up like a bubble, call all onclick methods.


However, when you click "hello", you also click the parent label of "hello, that is, the div tag above, and also the parent tag that includes the div tag, that is, another div. So I thought, it wasn't actually the so-called bubble, but you triggered the same event of multiple tags.


So I don't need to use it next. Let's look at the example below.


<Html>

<Head>

<Script>

Function f1 (){

Alert ('f1 ');

}

Function f2 (){

Alert ('F2 ');

}

Function f3 (){

Alert ('f3 ');

}

</Script>

</Head>

<Body>

<Div style = "background-color: red; width: 200px; height: 200px;">

<Div style = "background-color: blue; width: 100px; height: 100px;">

<A onmouseout = "f1 ();"> Hello </a>

</Div>

</Div>

</Body>

</Html>



Yes, the difference from the above Code is to change onclick to onmouseout. How can we test it?

Place your mouse on the "hello" text, move it out, and move it out of the size range of the "hello" div, so that you can trigger it, the <a> onmouseout event of the tag is not triggered. The onmouseout event of the external div is. The test result is,



Unexpectedly, the f1 f2 f3 still pops up. That is to say, no matter whether you trigger an event with the external parent tag or not, you will call the relevant function. That is to say, the name of the bubble mechanism is reasonable. I think too much about it. I also hope that some teachers in the js course can clearly explain what these cainiao may not understand very well, thank you for watching.

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.