Home Web Front-end JS Tutorial Why does event bubbling trigger multiple times?

Why does event bubbling trigger multiple times?

Feb 24, 2024 pm 08:33 PM
Event bubbling repeat trigger

Why does event bubbling trigger multiple times?

Why is the event bubbling triggered twice?

In front-end development, we often encounter the concept of event bubbling. Event bubbling means that when a specific event of an element is triggered on the page, the event will be passed layer by layer to the upper element until it is finally passed to the document object.

However, sometimes we may encounter the problem of event bubbling and triggering twice, even if we only bind the event listener once. So why does the phenomenon of repeated triggering occur? Let’s dive into the possible reasons below.

First of all, we need to clarify a concept, that is, how event bubbling works in the browser. When the browser triggers a specific event on an element, it will traverse the DOM tree starting from the element and check whether each parent element has an event handler bound to the event. If an event handler is found, the browser executes the handler and continues up the traversal up to the document object. This process is event bubbling.

However, it should be noted that event bubbling will not only be passed to the parent element, but also to the ancestor elements. That is, event bubbling repeatedly triggers event handlers on parent and ancestor elements. This may cause the event to be triggered twice.

So the question is, why does event bubbling pass to ancestor elements? This is because when binding event listeners, we usually use event delegation. That is to say, bind the event listener to the parent element, and then let the parent element handle the event instead of its child element through event bubbling.

If the event listener we bind exists on both the parent element and the ancestor element, the event bubbling will be triggered twice. This is because as the browser traverses up the DOM tree, it checks whether each parent element and ancestor element has the same event handler bound to it. If present, the browser will execute them.

In order to better understand this problem, a specific code example is provided below:

HTML code:

<div id="parent">
  <div id="child">Click me!</div>
</div>
Copy after login

JavaScript code:

var parent = document.getElementById('parent');
var child = document.getElementById('child');

parent.addEventListener('click', function() {
  console.log('Parent Clicked!');
});

child.addEventListener('click', function() {
  console.log('Child Clicked!');
});
Copy after login

above In the code, we bind event listeners for click events on both the parent element and the child element. When we click on the child element, the console will output the results twice: "Child Clicked!" and "Parent Clicked!". This is because the event bubbling is triggered twice, first the event listener of the child element is executed, and then the event listener of the parent element is executed.

In order to avoid the problem of event bubbling being triggered twice, we can use the stopPropagation() method to prevent the event from continuing to bubble up. For example, in the above code, we can add a line of code in the event listener of the child element:

child.addEventListener('click', function(event) {
  console.log('Child Clicked!');
  event.stopPropagation();
});
Copy after login

After using the stopPropagation() method, only the event listener of the child element is executed, and the event of the parent element The listener will no longer be executed. In this way, we can avoid the problem of event bubbling and triggering twice.

To summarize, the reason why event bubbling will be triggered twice is because the event bubbling will be passed to the event handlers on the parent element and ancestor elements. To solve this problem, we can use the stopPropagation() method to prevent the event from bubbling upward.

I hope that through the introduction of this article, readers can have a more comprehensive understanding of the principle of event bubbling and why it is triggered twice, and can find an effective solution when encountering this problem in actual development. Method.

The above is the detailed content of Why does event bubbling trigger multiple times?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Understand the event bubbling mechanism: Why does a click on a child element affect the event of the parent element? Understand the event bubbling mechanism: Why does a click on a child element affect the event of the parent element? Jan 13, 2024 pm 02:55 PM

Understanding event bubbling: Why does a click on a child element trigger an event on the parent element? Event bubbling means that in a nested element structure, when a child element triggers an event, the event will be passed to the parent element layer by layer like bubbling, until the outermost parent element. This mechanism allows events on child elements to be propagated throughout the element tree and trigger all related elements in turn. To better understand event bubbling, let's look at a specific example code. HTML code: <divid="parent&q

Reasons and solutions for jQuery .val() failure Reasons and solutions for jQuery .val() failure Feb 20, 2024 am 09:06 AM

Title: Reasons and solutions for the failure of jQuery.val() In front-end development, jQuery is often used to operate DOM elements. The .val() method is widely used to obtain and set the value of form elements. However, sometimes we encounter situations where the .val() method fails, resulting in the inability to correctly obtain or set the value of the form element. This article will explore the causes of .val() failure, provide corresponding solutions, and attach specific code examples. 1.Cause analysis.val() method

Why does event bubbling trigger twice? Why does event bubbling trigger twice? Feb 22, 2024 am 09:06 AM

Why does event bubbling trigger twice? Event bubbling (Event Bubbling) means that in the DOM, when an element triggers an event (such as a click event), the event will bubble up from the element to the parent element until it bubbles to the top-level document object. . Event bubbling is part of the DOM event model, which allows developers to bind event listeners to parent elements, so that when child elements trigger events, the events can be captured and processed through the bubbling mechanism. However, sometimes developers encounter events that bubble up and trigger twice.

Why can't click events in js be executed repeatedly? Why can't click events in js be executed repeatedly? May 07, 2024 pm 06:36 PM

Click events in JavaScript cannot be executed repeatedly because of the event bubbling mechanism. To solve this problem, you can take the following measures: Use event capture: Specify an event listener to fire before the event bubbles up. Handing over events: Use event.stopPropagation() to stop event bubbling. Use a timer: trigger the event listener again after some time.

Which JS events don't bubble up? Which JS events don't bubble up? Feb 19, 2024 pm 09:56 PM

What are the situations in JS events that will not bubble up? Event bubbling (Event Bubbling) means that after an event is triggered on an element, the event will be transmitted upward along the DOM tree starting from the innermost element to the outermost element. This method of transmission is called event bubbling. However, not all events can bubble up. There are some special cases where events will not bubble up. This article will introduce the situations in JavaScript where events will not bubble up. 1. Use stopPropagati

Why does the event bubbling mechanism trigger twice? Why does the event bubbling mechanism trigger twice? Feb 25, 2024 am 09:24 AM

Why does event bubbling happen twice in a row? Event bubbling is an important concept in web development. It means that when an event is triggered in a nested HTML element, the event will bubble up from the innermost element to the outermost element. This process can sometimes cause confusion. One common problem is that event bubbling occurs twice in a row. In order to better understand why event bubbling occurs twice in a row, let's first look at a code example:

How to extract only one piece of duplicate data in Oracle database? How to extract only one piece of duplicate data in Oracle database? Mar 09, 2024 am 09:03 AM

How to extract only one piece of duplicate data in Oracle database? In daily database operations, we often encounter situations where we need to extract duplicate data. Sometimes we want to find one of the duplicate data instead of listing all the duplicate data. In Oracle database, we can achieve this purpose with the help of some SQL statements. Next, we will introduce how to extract only one piece of duplicate data from the Oracle database and provide specific code examples. 1. Use ROWID function ROWID is Ora

What scenarios can event modifiers in vue be used for? What scenarios can event modifiers in vue be used for? May 09, 2024 pm 02:33 PM

Vue.js event modifiers are used to add specific behaviors, including: preventing default behavior (.prevent) stopping event bubbling (.stop) one-time event (.once) capturing event (.capture) passive event listening (.passive) Adaptive modifier (.self)Key modifier (.key)

See all articles