Event bubbling and event capture for JS

Source: Internet
Author: User
Tags id3

JS Bubbles the event by default and begins capturing all of the listener objects for that event from the root element. We can specify whether the event executes in the bubbling or capturing phase by binding the event.

Obj.addeventlistener (Event,function () {},bool);
Bool:false, represents the bubbling phase execution
Bool:true, representing capture phase execution

Example:

<ul>
            <li>item1</li>
            <li>item2</li>
            <li>item3</li>
            <li>item4</li>
            <li>item5</li>
            <li>item6</li>
        </ul>

For example, there are UL events and Li events, click on the event capture after Li

From top to bottom to trigger events, first trigger the UL event, and then trigger Li incident

document.getElementById ("ParentID"). AddEventListener ("click", Function (e) {
    alert (' Trigger parent Event ');
}, true;
Event Bubbling

bottom-up to trigger the event, triggering the Li event, and then triggering the UL event to prevent the event bubbling

The method of the consortium is e.stoppropagation (), ie is using e.cancelbubble = true;

<script type= "Text/javascript" >  
    var obj1=document.getelementbyid (' Id1 ');   
    Obj1.addeventlistener (' click ', Function (e) {curclick (' id1 '); Stoppropagation (e)},false);  

    var Obj2=document.getelementbyid (' Id2 ');  
    Obj2.addeventlistener (' click ', Function (e) {curclick (' id2 '); Stoppropagation (e)},true);  

    var Obj3=document.getelementbyid (' id3 ');  
    Obj3.addeventlistener (' click ', Function (e) {curclick (' id3 '); Stoppropagation (e)},true);  

    function Curclick (ID) {  
        alert (ID);  
    }  

    function  Stoppropagation (e) {  
        var e = window.event | | event;    
        if (e.stoppropagation) {//w3c block bubbling Method    
            e.stoppropagation ();    
        } else {    
            e.cancelbubble = true;//ie Block bubbling method    
        }   
    }      
</script>  

When clicked ID3: Execution Result: Id2
When clicked Id2: Execution Result: Id2
When clicked Id1: Execution Result: ID1
By This example, the event bubbling was prevented and the event continued to be captured from the subordinate.

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.