亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
2. Event Capturing: From Outer to Inner
3. The Full Event Flow: Three Phases
4. How to Control the Flow
Practical Use Cases
Home Web Front-end JS Tutorial What is event bubbling and event capturing in JavaScript?

What is event bubbling and event capturing in JavaScript?

Aug 01, 2025 am 05:31 AM
Event bubbling

The propagation of events in the DOM is divided into two stages: capture and bubble. The answer is: 1. Event capture is transmitted from the outer layer to the inner layer, 2. Event bubble is transmitted from the inner layer to the outer layer, 3. The complete process is the capture stage → target stage → bubble stage, 4. The process can be controlled through stopPropagation and other methods, 5. The practical application includes event delegation and click external closing functions. Understanding the two helps to write efficient and predictable event processing code.

What is event bubble and event capturing in JavaScript?

Event bubble and event capturing are two phases of event propagation in the DOM (Document Object Model) that determine the order in which elements receive events when a user interacts with nested elements.

What is event bubble and event capturing in JavaScript?

Let's say you have a button inside a div , and both have click event listeners. When you click the button, that click also technically happens on the div — since the button is inside it. So, which event runs first? That's where event bubblering and capturing come in.


1. Event Bubbling: From Inner to Outer

In event bubbleing , the event starts at the innermost target element (like the button) and then bubbles up through its ancestors (parent, grandparent, etc.) until it reaches the root of the document.

What is event bubble and event capturing in JavaScript?

Example:

 <div id="outer">
  <div id="inner">
    Click me
  </div>
</div>
 document.getElementById("inner").addEventListener("click", () => {
  console.log("Inner clicked");
});

document.getElementById("outer").addEventListener("click", () => {
  console.log("Outer clicked");
});

When you click on the inner div , you'll see:

What is event bubble and event capturing in JavaScript?
 Inner clicked
Outer clicked

This is event bubbleing — the event goes from the child to the parent.

? This is the default behavior in most cases.


2. Event Capturing: From Outer to Inner

In event capturing , the event starts from the outermost element and travels down to the target element.

To enable capturing, you pass true as the third argument in addEventListener , or explicitly set the capture option.

 document.getElementById("inner").addEventListener("click", () => {
  console.log("Inner clicked");
}, true); // &#39;true&#39; means useCapture

document.getElementById("outer").addEventListener("click", () => {
  console.log("Outer clicked");
}, true);

Now, clicking the inner div will log:

 Outer clicked
Inner clicked

Because the event is captured from the top down.

? Capturing happens first , then bubble (if not stopped).


3. The Full Event Flow: Three Phases

An event actually goes through three phases :

  1. Capturing Phase – event goes from window down to the parent of the target.
  2. Target Phase – event reaches the target element.
  3. Bubbling Phase – event bubbles up from the target to window .

Example:

 <div id="grandparent">
  <div id="parent">
    <button id="button">Click</button>
  </div>
</div>

With listeners on all three, and capturing enabled on some:

  • Capturing listeners (with true ) fire during the capturing phase (top → target).
  • Bubbling listeners (default) fire during the bubble phase (target → top).

4. How to Control the Flow

You can stop propagation at any point:

  • event.stopPropagation() – stops the event from moving further in either phase.
  • event.stopImmediatePropagation() – also prevents other listeners on the same element from running.

Example:

 button.addEventListener("click", function(e) {
  e.stopPropagation();
  console.log("Button clicked, but won&#39;t bubble");
});

Now, the parent won't receive the click event.


Practical Use Cases

  • Event Delegation : Use bubble to attach a single listener on a parent to handle events for many children (eg, a list with many buttons).
  • Click Outside to Close : Attach a capturing listener on the body to detect clicks outside a modal or dropdown.
     document.body.addEventListener("click", (e) => {
      if (!modal.contains(e.target)) {
        modal.close();
      }
    }, true); // Capturing ensures it runs before any inner bubble listeners

    In short:

    • Bubbling : Inner → Outer (default).
    • Capturing : Outer → Inner (use true in addEventListener ).
    • The full flow is: Capture → Target → Bubble .

    Understanding both helps you write more efficient and predictable event handling code.

    The above is the detailed content of What is event bubbling and event capturing in JavaScript?. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

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

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

What are the common ways to prevent bubbling events? What are the common ways to prevent bubbling events? Feb 19, 2024 pm 10:25 PM

What are the commonly used commands to prevent bubbling events? In web development, we often encounter situations where we need to handle event bubbling. When an event is triggered on an element, such as a click event, its parent element will also trigger the same event. This behavior of event delivery is called event bubbling. Sometimes, we want to prevent an event from bubbling up, so that the event only fires on the current element, and prevents it from being passed to superior elements. To achieve this, we can use some common directives that prevent bubbling events. event.stopPropa

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.

Practical tips and case studies for preventing incidents from bubbling up Practical tips and case studies for preventing incidents from bubbling up Jan 13, 2024 pm 03:28 PM

Practical tips and case studies to prevent event bubbling. Event bubbling means that in the DOM tree, when an element triggers an event, the event will bubble up to the parent element in the DOM tree, all the way to the root node. This bubbling mechanism can sometimes lead to unexpected problems and errors. In order to avoid this problem from happening, we need to learn to use some practical techniques to prevent events from bubbling up. This article will introduce some commonly used techniques to prevent event bubbling, analyze them with cases, and provide specific code examples. 1. Use st of event object

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.

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)

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:

See all articles