JS bubbling event and Event capture example detailed _javascript tips

Source: Internet
Author: User
Tags time and date

This article analyzes the JS bubbling event and event capture. Share to everyone for your reference, specific as follows:

Case

<! DOCTYPE html>
 
 

Summary

1. Click Mydiv. In turn, Div-body-html-document-window.

2. Click on other blank places. In turn, Html-document-window.

3. Two consecutive clicks, will be executed in order twice

4. The premise of bubbling is that the parent also defines the corresponding event

Let's look at a more complicated example.

&lt;! DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset= "Utf-8" &gt; &lt;title&gt; event capture &lt;/title&gt; &lt;script t Ype= "Text/javascript" &gt; window.onload = function () {Window.addeventlistener ("click", Function () {alert
      ("Window-true"); }, True); True-the event handle executes in the capture phase, false-false-default.
      The event handle executes Document.addeventlistener in the Bubbling phase ("click", Function () {alert ("Document-true");
      }, True);
      Document.documentElement.addEventListener ("click", Function () {alert ("Html-true");
      }, True);
      Document.body.addEventListener ("click", Function () {alert ("Body-true");
      }, True);
      document.getElementById ("Mydiv"). AddEventListener ("click", Function () {alert ("Div-true");
      }, True);
      Window.addeventlistener ("click", Function () {alert ("Window-false"); }, False); True-the event handle executes in the capture phase, false-false-default. The event handle executes Document.addeventlistener in the Bubbling phase ("click", Function () {alert("Document-false");
      }, False);
      Document.documentElement.addEventListener ("click", Function () {alert ("Html-false");
      }, False);
      Document.body.addEventListener ("click", Function () {alert ("Body-false");
      }, False);
      document.getElementById ("Mydiv"). AddEventListener ("click", Function () {alert ("Div-false");
      }, False);
      Window.onclick = function () {alert ("Window-click");//top-level};
      Document.onclick = function () {alert ("Document-click");//secondary top-level};
      Document.documentElement.onclick = function () {alert ("Html-click");//secondary top-level};
      Document.body.onclick = function () {alert ("Body-click");//secondary top level}; document.getElementById ("Mydiv"). onclick = function () {alert ("Div-click");//First executed, and passed to the previous level. Click two times, then execute two times in order.
    If the superior also has the Click event}; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id= "mydiv" &gt; Point I &lt;/div&gt; &lt;/body&gt; &LT;/html&gt;

 

Summary

1. The results of the click execution are

Window-true

Document-true

Html-true

Body-true

Div-true

Div-false

Div-click

Body-false

Body-click

Html-false

Html-click

Document-false

Document-click

Window-false

Window-click

2. Order is independent of JS code order

3. Even if you do not define a click event, you can capture the click event, as long as you click on it to capture

Remake again, stop bubbling

&lt;! DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset= "Utf-8" &gt; &lt;title&gt; event capture &lt;/title&gt; &lt;script t Ype= "Text/javascript" &gt; window.onload = function () {Document.addeventlistener ("click", Function () {Ale
      RT ("Document-true");
      }, True);
      Window.addeventlistener ("click", Function () {alert ("Window-true"); }, True); True-the event handle executes in the capture phase, false-false-default.
      The event handle executes Document.documentElement.addEventListener in the Bubbling phase ("click", Function () {alert ("Html-true");
      }, True);
      Document.body.addEventListener ("click", Function () {alert ("Body-true");
      }, True);
      document.getElementById ("Mydiv"). AddEventListener ("click", Function () {alert ("Div-true");
      }, True);
      Window.addeventlistener ("click", Function () {alert ("Window-false"); }, False); True-the event handle executes in the capture phase, false-false-default. The event handle executes Document.addeventlistener in the Bubbling phase ("click", Function () {alert("Document-false");
      }, False);
      Document.documentElement.addEventListener ("click", Function () {alert ("Html-false");
      }, False);
      Document.body.addEventListener ("click", Function () {alert ("Body-false");
      }, False);
      document.getElementById ("Mydiv"). AddEventListener ("click", Function () {alert ("Div-false");
      }, False);
      Window.onclick = function () {alert ("Window-click");//top-level};
      Document.onclick = function () {alert ("Document-click");//secondary top-level};
      Document.documentElement.onclick = function () {alert ("Html-click");//secondary top-level};
      Document.body.onclick = function () {alert ("Body-click");//secondary top level}; document.getElementById ("Mydiv"). onclick = function () {alert ("Div-click");//First executed, and passed to the previous level. Click two times, then execute two times in order. If the superior also has the Click event Event.stoppropagation ();
    Block bubbling}; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id= "Mydiv "&gt; Point I &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;

 

Summary

Window-true

Document-true

Html-true

Body-true

Div-true

Div-false

Div-click

Terminated, no further content.

For more information on JavaScript-related content readers can view the site topics: "JavaScript events related operations and Skills encyclopedia", "JavaScript time and date Operation skills Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills Summary, "JavaScript animation effects and techniques Summary", "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and skills summary" and The summary of JavaScript mathematical operational usage

I hope this article will help you with JavaScript programming.

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.