Home Web Front-end JS Tutorial Introduction and application of JavaScript event bubbling_javascript skills

Introduction and application of JavaScript event bubbling_javascript skills

May 16, 2016 pm 06:36 PM
Event bubbling

1. What is event bubbling?
Trigger a certain type of event on an object (such as a click onclick event). If the object defines a handler for this event, then this event will call this Handler, if this event handler is not defined or the event returns true, then this event will be propagated to the parent object of this object, from inside to outside, until it is handled (all similar events of the parent object will be activated), Or it reaches the top level of the object hierarchy, which is the document object (window in some browsers).

For example: you want to appeal a case in the local court. If there is no local court to handle such cases, the relevant local departments will help you continue to appeal to the higher court, such as from the municipal level to the provincial level. , until you go to the Central Court to finally get your case processed.

2. What is the role of event bubbling?
(1) Event bubbling allows multiple operations to be processed centrally (add event handlers to a parent element to avoid Adding event handlers to multiple child elements) also allows you to capture events at different levels of the object layer.

[Centralized processing example]
Copy code The code is as follows:

< ;div onclick="eventHandle(event)" id="outSide" style="width:100px; height:100px; background:#000; padding:50px">




(2) Let different objects capture the same event at the same time and call their own exclusive handlers to do their own things, like The boss gave an order and each employee went to do his or her job.

[Example of capturing the same event at the same time]
Copy code The code is as follows:






3. What you need to pay attention to
●There are actually three ways to capture events, and event bubbling is just one of them: (1) IE bubbling events from inside to outside (inside→outside). (2) Netscape4.0 capture events from outside to inside (outside→inside). (3) DOM event flow, first from the outside to the inside, and then from the inside to the outside back to the origin (outside→inside→outside) event capture method (it seems that the object will trigger event processing twice, what is the effect of this? I don’t understand ! ).

●Not all events can bubble up. The following events do not bubble: blur, focus, load, unload.

●Event capture methods are different in different browsers, or even different versions of the same browser. For example, Netscape 4.0 uses a capture event solution, and most other browsers support a bubbling event solution. In addition, the DOM event stream also supports text node event bubbling.

●The target of event capture to reach the top level is also different in different browsers or different browser versions. In IE6, HTML receives event bubbling, and most browsers continue bubbling to the window object, that is...body→documen→window.

●Preventing bubbling does not prevent the object's default behavior. For example, when the submit button is clicked, the form data will be submitted. This behavior does not require us to write a customized program.

4. Prevent events from bubbling up
Usually we do it all in one step, clarifying the source of our event triggers, and do not want the browser to be smart and aimlessly help us find appropriate event processing Program, that is, we know the most precise goal. In this case, we do not need event bubbling. In addition, through the understanding of event bubbling, we know that the program will do more extra things, which will inevitably increase the program overhead. Another important issue is that event bubbling processing may activate events that we do not want to activate, causing program confusion and even making it impossible to debug. This often becomes a thorny issue for programmers who are not familiar with event bubbling. So when necessary, we need to prevent events from bubbling up.

[Example of activation of events that do not want to be activated]
Copy code The code is as follows:

< /div> Open the Baidu homepage, but when you click on the gray box, two web pages are opened at the same time. In fact, this problem is rarely encountered in actual design. You may think that if I place different buttons or links in different DOM depths of the page, will the event triggering in the deep layers affect the top-level buttons? No, because buttons cannot form nested relationships.
function openWin(url)
{
window.open(url);
}



The following is what I copied from the Internet A method. Place this method at the end of the precise target object handler. After this event is triggered and processed, the event will no longer be bubbled.

[Example of preventing event bubbling]




Copy code


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:

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)

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

See all articles