Detailed explanation of javascript event bubbling, event capture and event delegation

Detailed explanation of javascript event bubbling, event capture and event delegation

1. Event bubbling : In the process of JavaScript event propagation, when an event is triggered on an element, the event will propagate to the predecessor elements step by step until the document, and some browsers may reach the window. Not all events have bubbling, for example: blur event, focus event, load event

2. Event delegation : Event capture is exactly the opposite of event bubbling. It starts from the top-level ancestor element until the event triggers the element.

js event capture is generally implemented through the DOM2 event model addEventListener :

target.addEventListener(type, listener, useCapture)

The third parameter is set to false by default, indicating that the event is triggered in the bubbling phase. When set to true, it is triggered in the capture phase. Generally, event capture seems to be rarely used in our work. But still need to understand

<div id="box">
    <div id="middle">
        <div id="inner"></div>
    </div>
</div>
<script>
//Event capture window.onload=function(){
    let box = document.getElementById("box");
    let middle = document.getElementById("middle");
    let inner = document.getElementById("inner");
    box.addEventListener("click",function(){console.log("box")},true);
    middle.addEventListener("click",function(){console.log("middle")},true);
    inner.addEventListener("click",function(){console.log("inner")},true);
}
</script>
Click inner, and the console will output: box, middle, inner

Stop event bubbling

Usually, a large number of event bubbling events are used, but we may not need to pass events to the parent in a certain child tag. At this time, we need to prevent the bubbling of its events.

Generally, stopPropagation is used to prevent the bubbling of events. In IE, cancelBuble=true is used. stopPropagation is also a method of the event object (Event). Its function is to prevent the bubbling event of the target element, but it will not prevent the default behavior.

//Prevent event bubbling let btna = document.getElementById('btn');
btna.onclick=function(e){
    window.event? window.event.cancelBubble = true : e.stopPropagation();
 };

3. Event delegation : Event delegation can also be called event proxy. Event delegation makes use of event bubbling. By specifying only one event handler, you can manage all events of a certain type.

Benefits: Reducing DOM operations can improve web page performance. When a parent element and many child elements of a page need to operate the same event, we cannot bind an event to each element.

<ul id="getNum">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
</ul>
<script>
let ptclick = document.getElementById('getNum');
let lilist = ptclick.querySelectorAll('li');
for(let i=0;i<lilist.length;i++){
    lilist[i].index = i;
};
ptclick.onclick = function(e){
    var e = e || window.event;
    var target = e.target || e.srcElement;
    console.log(e.target.index);
};
</script>

Summarize

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

You may also be interested in:
  • A brief analysis of event bubbling and event capture in js
  • Detailed Analysis of Event Bubbling Mechanism in JavaScript
  • Introduction to event bubbling and event capture in JS
  • Detailed explanation of JS bubbling events and event capture examples
  • Detailed explanation of js event delegation
  • JavaScript adds event listeners to event delegation in batches. Detailed process

<<:  Some small methods commonly used in html pages

>>:  Summary of some tips on MySQL index knowledge

Recommend

Detailed explanation of system input and output management in Linux

Management of input and output in the system 1. U...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

A line of CSS code that crashes Chrome

General CSS code will only cause minor issues wit...

Detailed explanation of MySQL 30 military rules

1. Basic Specifications (1) InnoDB storage engine...

ReactHooks batch update state and get route parameters example analysis

Table of contents 1. How to update in batches Con...

Understanding MySQL precompilation in one article

1. Benefits of precompilation We have all used th...

jQuery canvas generates a poster with a QR code

This article shares the specific code for using j...

Solution to 2059 error when connecting Navicat to MySQL

Recently, when I was learning Django, I needed to...

Detailed explanation of the difference between JavaScript onclick and click

Table of contents Why is addEventListener needed?...

Detailed steps for configuring virtual hosts in nginx

Virtual hosts use special software and hardware t...