Home Web Front-end JS Tutorial Detailed summary of js bubbling, capturing events and preventing bubbling methods_javascript skills

Detailed summary of js bubbling, capturing events and preventing bubbling methods_javascript skills

May 16, 2016 pm 04:49 PM
bubble capture events Stop bubbling

There are problems with event bubbling and event capture in JavaScript and jquery events. The two problems and their solutions are summarized in detail below.

Event bubbling is a process of bubbling from child nodes to ancestor nodes;

Event capture is just the opposite, a process from ancestor nodes to child nodes.

Give an example of jquery click event:

The code is as follows:

Copy code Code As follows:



test









Event bubbling phenomenon: Click the button with "id=clickMe", and two pop-up boxes "hello" and "baby" will appear successively.

Analysis: When the button with "id=clickMe" is clicked, the click event bound to the button, button parent element and body is triggered, so two boxes pop up one after another, causing the so-called bubbling phenomenon.


Event capture phenomenon: When you click on a div that has no click event bound and a button with "id=button2", the "baby" dialog box will pop up.


In actual projects, we need to prevent event bubbling and event capturing.

Methods to prevent event bubbling:

Method 1: return false in the current click event;
Copy code The code is as follows:

$('#clickMe').click(function(){
alert('hello');

return false;
});

Method 2:
Copy code The code is as follows:

$('#clickMe').click(function(event){
alert('hello');

var e = window.event || event;

if ( e.stopPropagation ){ //If the event object is provided, this is a non-IE browser
e.stopPropagation();
}else{
//An IE-compatible way Cancel event bubbling
window.event.cancelBubble = true;
}
});

It seems that capturing events cannot be blocked
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)

Which browsers support js capture events Which browsers support js capture events Nov 13, 2023 pm 02:42 PM

Browsers that support js capture events include "Google Chrome", "Mozilla Firefox", "Safari", "Microsoft Edge" and "Opera" and all other mainstream browsers.

Master the common event bubbling mechanism in JavaScript Master the common event bubbling mechanism in JavaScript Feb 19, 2024 pm 04:43 PM

Common bubbling events in JavaScript: To master the bubbling characteristics of common events, specific code examples are required. Introduction: In JavaScript, event bubbling means that the event will start from the element with the deepest nesting level and propagate to the outer element until it propagates to The outermost parent element. Understanding and mastering common bubbling events can help us better handle user interaction and event handling. This article will introduce some common bubbling events and provide specific code examples to help readers better understand. 1. Click event (click

How to effectively prevent events from bubbling up How to effectively prevent events from bubbling up Feb 19, 2024 pm 08:25 PM

How to effectively prevent event bubbling requires specific code examples. Event bubbling means that when an event on an element is triggered, the parent element will also receive the same event trigger. This event delivery mechanism sometimes brings problems to web development. Trouble, so we need to learn how to effectively stop events from bubbling up. In JavaScript, we can stop an event from bubbling by using the stopPropagation() method of the event object. This method can be called within an event handler to stop the event from propagating to the parent element.

Which JS events are not propagated upward? Which JS events are not propagated upward? Feb 19, 2024 am 08:17 AM

Which JS events will not bubble? In JavaScript, event bubbling means that when an element triggers an event, the event will bubble up to higher-level elements until it bubbles to the document root node. The event handlers are then executed in the order they bubble up. However, not all events bubble up. Some events will only execute the event handler on the target element after being triggered, without bubbling up to higher-level elements. Here are some common events that do not bubble: focus and blur events:

Why does the same event trigger bubbling twice? Why does the same event trigger bubbling twice? Feb 19, 2024 pm 10:34 PM

Why does the same bubbling event happen twice? Event bubbling is a common event delivery mechanism in browsers. When an element triggers an event, the event will be passed from the triggered element to the upper elements in sequence until it is passed to the root element of the document. This process is like bubbles bubbling in water, so it is called event bubbling. However, sometimes we find that the same bubbling event occurs twice. Why is this? There are two main reasons: event registration and event processing. First, we need to make it clear that the event

Capture first or bubble first? Analyze the advantages and disadvantages of event processes Capture first or bubble first? Analyze the advantages and disadvantages of event processes Feb 21, 2024 pm 02:36 PM

Capture first or bubble first? Analyzing the advantages and disadvantages of event process Event process is an important concept in web development. It describes the process of events from occurrence to processing. There are two main process models when handling events: capture then bubble and bubble then capture. These two models have their own advantages and disadvantages in different scenarios, and you need to choose the appropriate model based on the actual situation. Capturing first and then bubbling means that the event capturing phase is executed before the event bubbling phase. The event capture phase starts from the root node of the event target and passes down step by step until it reaches the target element.

Learn more about event bubbling and capturing in JavaScript Learn more about event bubbling and capturing in JavaScript Aug 04, 2022 pm 09:01 PM

This article will take you through event bubbling and capturing, and let you understand the js event target search method (bubbling and capturing), event proxy, the difference between e.target and e.currentTarget, preventing bubbling and capturing, and canceling the default event. ,I hope to be helpful!

Explore click event bubbling and master the key principles of front-end development Explore click event bubbling and master the key principles of front-end development Jan 13, 2024 am 10:56 AM

Learn click event bubbling and master key concepts in front-end development. Specific code examples are required. Front-end development is an important field in today's Internet era, and event bubbling is one of the key concepts in front-end development. Understanding and mastering event bubbling is critical to writing efficient front-end code. This article will introduce what event bubbling is and how to use the concept of event bubbling in front-end development. 1. What is event bubbling? Event bubbling means that when an event on an element is triggered, it will start from the innermost element first, and then proceed to the parent element step by step.

See all articles