JS event bubbling, event capture

Source: Internet
Author: User

The AddEventListener () method can specify the "usecapture" parameter to set the delivery event type:false→ bubble true→ captures the default false.

The inner element is the p tag, the outer element is the div tag, and the experiment is as follows:

In bubbling, an inner element's event is first triggered before the outer element is triggered, in order: internal ==> External

CSS:. whb100{width:100px;height:100px;border:1px dotted red}.whb200{width:200px;height:200px;border:1px Dotted #2ea6df}.center{margin-left:25%;margin-top:25%;} HTML:<div  class= "whb200 usecapturefalse" id= "" >      <p  class= "whb100 Center UseCaptureFalse2 "></p></div>js:var usecapturefalse=document.queryselector (". Usecapturefalse ") var usecapturefalse2=document.queryselector (". UseCaptureFalse2 ") Usecapturefalse2.addeventlistener ("click", Function () {    alert ("you clicked P Element!");}, False); Usecapturefalse.addeventlistener ("click", Function (event) {    alert ("You clicked on the div element!");    Console.log (Event)}, False);

In a capture, an event of an external element is first triggered and then triggered inside an outer element, in order: external ==> Internal

CSS:. whb100{width:100px;height:100px;border:1px dotted red}.whb200{width:200px;height:200px;border:1px Dotted #2ea6df}.center{margin-left:25%;margin-top:25%;} HTML:<div  class= "whb200 usecapturefalse" id= "" >      <p  class= "whb100 Center UseCaptureFalse2 "></p></div>js:var usecapturefalse=document.queryselector (". Usecapturefalse ") var usecapturefalse2=document.queryselector (". UseCaptureFalse2 ") Usecapturefalse2.addeventlistener ("click", Function () {    alert ("You clicked on P2 element!");}, False); Usecapturefalse.addeventlistener ("click", Function (event) {    alert ("You clicked Div2 Element!");    Console.log (Event)}, False);

 

JS event bubbling, event capture

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.