AddEventListener () detailed analysis of the third parameter useCapture (Boolean) _ basic knowledge

Source: Internet
Author: User
The trigger order of true is always before false; if multiple are true, the trigger of the outer layer prevails over the inner layer; if multiple are false, the trigger of the inner layer prevails over the outer layer. Example








If you click the mouse on d3, the event stream is like this:

The capture phase checks whether there are Event Handlers whose useCapture is true at p1. If so, it executes the program and then processes p2 in the same way.

When the target stage is at p3, it is found that p3 is the node clicked by the mouse. Therefore, this stage is the target stage. If there is an event processing program, execute this program, regardless of whether useCapture is true or false.

In the bubble stage, p2 checks whether there are Event Handlers whose useCapture is false. If so, execute the program and then process p1 in the same way.

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.



Click here.





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" +"
";}, False );
MiddleDiv. addEventListener ("click", function () {info. innerHTML + = "middleDiv" +"
";}, False );
InDiv. addEventListener ("click", function () {info. innerHTML + = "inDiv" +"
";}, 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.

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.