Detailed explanation of Javascript event capture and bubbling methods

Detailed explanation of Javascript event capture and bubbling methods

1. Event Processing Model

Event bubbling and event capturing : Event bubbling and event capturing were proposed by Microsoft and Netscape respectively. Both concepts are intended to solve the problem of event flow (the order in which events occur) in the page.

<div id="d1">
        <div id="d2">
            <div id="d3"></div>
        </div>
</div>

Given three nested divs, when the same event is registered for the three elements, what is their triggering order?

1. Event bubbling

Microsoft proposed an event stream called event bubbling. Elements that are structurally (not visually) nested will have a bubbling function, that is, the same event will bubble from the child element to the parent element. (Bottom-up)

For the above example, if the bubbling method is used, the triggering order should be: d3——>d2——>d1, so let's verify it:

(1) Bind events to three div elements

//1. Get the element var d1 = document.querySelector('#d1')
var d2 = document.querySelector('#d2')
var d3 = document.querySelector('#d3')
//2. Binding event d1.onclick = function(){
     console.log(this.id)
  }
d2.onclick = function(){
      console.log(this.id)
  }
d3.onclick = function(){
      console.log(this.id)
   }

(2) Operation results:

Click on the red area:

Click on the purple area:

Click on the green area:

The above is the event bubbling!

2. Event Capture

Elements that are structurally (not visually) nested will have the function of event capture, that is, the same event is captured from the parent element to the child element (event source element). (Top-down) (ie no events are captured)

For the above example, if the bubbling method is used, the triggering order should be: d1——>d2——>d3, so let's verify it:

(1) Bind events to three div elements

//1. Get the element var d1 = document.querySelector('#d1')
var d2 = document.querySelector('#d2')
var d3 = document.querySelector('#d3')
//2. Binding event d1.onclick = function(){
     console.log(this.id)
  }
d2.onclick = function(){
      console.log(this.id)
  }
d3.onclick = function(){
      console.log(this.id)
   }

(2) Operation results:

Click on the red area:

Click on the purple area:

Click on the green area:

Event capture get!!!

Notice:

  • Trigger order: capture first, then bubbling
  • Events such focus , blur , change , submit , reset , select etc. do not bubble

2. Prevent event bubbling

(1) W3C standard event.stopPropagation(); but IE9 and below do not support it

//1. Get the element var d1 = document.querySelector('#d1')
var d2 = document.querySelector('#d2')
var d3 = document.querySelector('#d3')
//2. Binding event d1.onclick = function(){
     console.log(this.id)
  }
d2.onclick = function(){
      console.log(this.id)
  }
d3.onclick = function(e){
      e.stopPropagation();
      console.log(this.id)
   }

You will find that when you click the green area, no external events are triggered in sequence, and event bubbling is blocked:

(2) Unique to IE: event.cancelBubble = true;

//1. Get the element var d1 = document.querySelector('#d1')
var d2 = document.querySelector('#d2')
var d3 = document.querySelector('#d3')
//2. Binding event d1.onclick = function(){
     console.log(this.id)
  }
d2.onclick = function(){
      console.log(this.id)
  }
d3.onclick = function(e){
      e.cancelBubble = true;
      console.log(this.id)
   }

The result is the same as (1).

(3) Merge cancellation: return false

In javascript, return false only prevents the default behavior, but with jQuery it prevents both the default behavior and object bubbling.

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Learn more about the most commonly used JavaScript events
  • Analysis of JavaScript's event loop mechanism
  • JavaScript event capture bubbling and capture details
  • Detailed explanation of js event delegation
  • A brief analysis of event bubbling and event capture in js
  • JavaScript event loop case study

<<:  Detailed explanation of four solutions to floating problems in CSS layout

>>:  Teach you how to deploy zabbix service on saltstack

Recommend

SQL IDENTITY_INSERT case study

Generally speaking, once a column in a data table...

Detailed explanation of HTML programming tags and document structure

The purpose of using HTML to mark up content is t...

Example code for text origami effect using CSS3

Preface This article mainly shares with you an ex...

Detailed explanation of JavaScript onblur and onfocus events

In HTML pages, visual elements such as buttons an...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

Understanding and usage scenarios of ES6 extension operators

Table of contents 1. Replace the apply method, ge...

CSS to achieve compatible text alignment in different browsers

In the front-end layout of the form, we often nee...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Shtml Concise Tutorial

Shtml and asp are similar. In files named shtml, s...

Vue implements a search box with a magnifying glass

This article shares with you how to use Vue to im...

Teach you how to quickly install Nginx in CentOS7

Table of contents 1. Overview 2. Download the Ngi...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

RGBA alpha transparency conversion calculation table

Conversion between rgba and filter values ​​under...

How to upgrade https under Nginx

Purchase Certificate You can purchase it from Ali...

MySQL fuzzy query statement collection

SQL fuzzy query statement The general fuzzy state...