UseCapture and addEventListener are the three parameters of addeventlistener.

Source: Internet
Author: User

UseCapture and addEventListener are the three parameters of addeventlistener.

AddEventListener has three parameters: the first parameter indicates the event name (not including on, such as "click"); the second parameter indicates the function for Receiving Event processing; the third parameter is useCapture, this article will explain it.

Copy codeThe Code is as follows:
<Div id = "outDiv">
<Div id = "middleDiv">
<Div id = "inDiv"> click here. </Div>
</Div>
</Div>
<Div id = "info"> </div>

Copy codeThe Code is as follows:
Var outDiv = document. getElementById ("outDiv ");
Var middleDiv = document. getElementById ("middleDiv ");
Var inDiv = document. getElementById ("inDiv ");
Var info = document. getElementById ("info ");
OutDiv. addEventListener ("click", function () {info. innerHTML + = "outDiv" + "<br>" ;}, false );
MiddleDiv. addEventListener ("click", function () {info. innerHTML + = "middleDiv" + "<br>" ;}, false );
InDiv. addEventListener ("click", function () {info. innerHTML + = "inDiv" + "<br>" ;}, false );

The above is the code we tested. Based on the display of info, we can determine the trigger order. There are three addEventListener options, and the optional values of useCapture are true and false, SO 2*2*2, we can get 8 different programs.

• When all values are false, the trigger sequence is inDiv, middleDiv, and outDiv;
• When all values are true, the trigger sequence is outDiv, middleDiv, and inDiv;
• When outDiv is true and others are false, the trigger sequence is outDiv, inDiv, and middleDiv;
• If middleDiv is set to true and other values are set to false, the trigger sequence is middleDiv, inDiv, and outDiv;
•......


Finally, the following conclusions are drawn:

• The trigger sequence of true is always before false;
• If multiple triggers are true, the triggering of the outer layer prevails over the inner layer;
• If multiple triggers are false, the triggering of the inner layer prevails over the outer layer.

The above is all the content of this article. I hope you will like it.

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.