An example analysis of event capture model and bubbling model in JS

Source: Internet
Author: User

The implementation methods are as follows:

Example 1:

code is as follows: <html>


<head>


<script type= "Text/javascript" >


window.onload = function () {


document.getElementById (' par '). AddEventListener (' click ', function () {alert (' par ');},true);


document.getElementById (' son '). AddEventListener (' click ', function () {alert (' son ');},true);


 }


</script>


<style type= "Text/css" >


#par {Width:300px;height:200px;background:gray}


#son {Width:200px;height:100px;background:green}


</style>


</head>


<body>


<div id= "par" >


<div id= "Son" ></div>


</div>


</body>


</html>


Example 2:

code is as follows: <html>


<head>


<script type= "Text/javascript" >


window.onload = function () {


document.getElementById (' par '). AddEventListener (' click ', function () {alert (' par ');});


document.getElementById (' son '). AddEventListener (' click ', function () {alert (' son ');});


 }


</script>


<style type= "Text/css" >


#par {Width:300px;height:200px;background:gray}


#son {Width:200px;height:100px;background:green}


</style>


</head>


<body>


<div id= "par" >


<div id= "Son" ></div>


</div>


</body>


</html>

AddEventListener: The third parameter is an optional parameter, which is false by default, which means the bubble model, which triggers the smallest layer (the div with the id son), and if you add the true parameter, it is the capture model (from the html-->body--- >DIV), triggering at such a level.

Instance 1 of the HTML code has two Div, small div contained in large Div, click on Small div, first trigger alert (' par ') event, then trigger alert (' son ') the whole piece. Instance 2 is just the opposite.

If you are using the object. onclick property to trigger the event, the bubble model is used.

IE does not support AddEventListener, but uses attachevent. But Attachevent does not support a third parameter, it does not capture the model.

I hope this article will help you with your JavaScript programming.

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.