Js-addeventlistener () the third parameter usecapture

Source: Internet
Author: User

Overview:

  The 3rd parameter, called Usecapture, is a Boolean value that is true or false. If it is true that the browser will use capture mode, false is bubbling, only in a certain situation, it is usually false, and it is the case that the target element has an ancestor element (a. ancestor element), and there is the same event response function

HTML fragment

    <DivID= "Div1"style= "background:blue;width:100px; height:100px;">        <DivID= "Div2"style= "background:red;width:70px; height:70px;">            <DivID= "Div3"style= "background:yellow;width:50px; height:50px;"></Div>        </Div>    </Div>

JS Code

varODVI1 = document.getElementById (' Div1 ')), ODvi2= document.getElementById (' Div2 '), ODvi3= document.getElementById (' Div3 ');//123, 456, 345Odvi1.addeventlistener (' Click ', xx1,true); Odvi2.addeventlistener (' Click ', XX2,false); Odvi3.addeventlistener (' Click ', XX3,true);functionXx1 () {//BlueAlert (123);}functionXx2 () {//RedAlert (345);}functionXX3 () {//YellowAlert (456);}

Summarize:

Trigger a click event on Div3

Capture stage: Outside-"in the DIV1 to detect whether Usecapture is true, is to execute the program, div2 the same."

Target stage: Target to Div3, find Div3 is the mouse click node, so this is the target phase. If there is an event handler, execute the program, regardless of whether usecapture is true or false.

Bubbling stage: Inside-the external DIV2 detects if the Usecapture is false and executes the program. Div1

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.