Home Web Front-end JS Tutorial Js bubbling event detailed explanation and blocking example_javascript skills

Js bubbling event detailed explanation and blocking example_javascript skills

May 16, 2016 pm 04:54 PM
bubbling event Prevent

The JS bubbling mechanism means that if an element defines an event A, such as a click event, and if the event is triggered and the bubbling event is not blocked, the event will propagate to the parent element and trigger the click function of the parent class.
As shown in the following example:

Copy the code The code is as follows:



<script> <br>function ialertdouble(e) { <br> alert('innerdouble'); <br>stopBubble(e); <br>} <br><br>function ialertthree(e) { <br>alert('innerthree'); <br>stopBubbleDouble(e); <br>} <br><br>function stopBubble(e) { <br>var evt = e||window.event; <br>evt.stopPropagation?evt.stopPropagation():(evt.cancelBubble=true);/ /Stop bubbling<br>} <br><br>function stopBubbleDouble(e) { <br>var evt = e||window.event; <br>evt.stopPropagation?evt.stopPropagation():(evt.cancelBubble =true);//Prevent bubbling<br>evt.preventDefault();//Prevent the browser’s default behavior so that the link will not jump<br>} <br><br>$(function() { <br>//Method 1<br>//$('#jquerytest').click(function(event) { <br>// alert('innerfour'); <br>// event.stopPropagation(); <br>// event.preventDefault(); <br>//}); <br><br>//Method 2<br>$('#jquerytest').click(function() { <br>alert( 'innerfour'); <br>return false; <br>}); <br>}); <br></script>
without
middle
inner

< div onclick="ialertdouble(event)">innerdouble

innerthree< ;/a>


innerfour






When you click inner, 'inner', 'middle' will pop up in sequence and 'without'. This is event bubbling.

Intuitively, this is also the case, because the innermost area is in the parent node. Clicking the area of ​​the child node actually clicks the area of ​​the parent node, so the event will Spread it.

Actually, many times, we don’t want events to bubble up, because this will trigger several events at the same time.

Next: we click innerdouble. You will find that she does not bubble because she calls the stopBubble() method in the calling method ialertdouble(). The method prevents bubbling by determining the browser type (Ie uses cancleBubble(), Firefox uses stopProgation()).

But if it is a link, we will find that it will also prevent bubbling, but will jump. This is the default behavior of the browser. You need to use the preventDefault() method to prevent it. See ialertthree() for details.

Currently, the mainstream method is to use jquery to bind click events. In this case, it will be much simpler.

We can pass in the parameter event when clicking the event, and then directly

event.stopPropagation();
event.preventDefault(); //No need to add this if there is no link.

That’s it.

The framework is good, but there is actually an easier way, returning false in the event handler. This is a shorthand way of calling stopPropagation() and preventDefault() on the event object at the same time.
[See the detailed code above, remember to load jquery.js. ]

In fact, you can also add judgment to each click event:
Copy code The code is as follows:

$('#id').click(function(event){
if(event.target==this){
//do something
}
})

Analysis: The variable event in the event handler stores the event object. The event.target attribute stores the target element where the event occurred. This attribute is specified in the DOM API, but is not implemented by all browsers. jQuery makes the necessary extensions to this event object so that this property can be used in any browser. With .target, you can determine the element in the DOM that first received the event (i.e. the element that was actually clicked). Moreover, we know that this refers to the DOM element that handles the event, so we can write the above code.

However, it is recommended to use return false if Jquery binds events.
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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
How to block access to websites in Edge How to block access to websites in Edge Jul 12, 2023 am 08:17 AM

Sometimes, we want to block certain websites on Microsoft Edge for many reasons, whether it is for parental control, time management, content filtering, or even security concerns. A common motivation is to be more productive and stay focused. By blocking distracting websites, people can create a conducive environment for working or studying, minimizing potential distractions. Finally, content filtering is important to maintaining a safe and respectful online environment. Blocking websites that contain explicit, offensive or objectionable content is particularly important in educational or professional settings where upholding appropriate standards and values ​​is crucial. If you can relate to this situation, this article is for you. Here’s how to block access to the Internet in Edge

How to effectively avoid memory leaks in closures? How to effectively avoid memory leaks in closures? Jan 13, 2024 pm 12:46 PM

How to prevent memory leaks in closures? Closure is one of the most powerful features in JavaScript, which enables nesting of functions and encapsulation of data. However, closures are also prone to memory leaks, especially when dealing with asynchronous and timers. This article explains how to prevent memory leaks in closures and provides specific code examples. Memory leaks usually occur when an object is no longer needed but the memory it occupies cannot be released for some reason. In a closure, when a function refers to external variables, and these variables

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

What events cannot bubble up? What events cannot bubble up? Nov 20, 2023 pm 03:00 PM

The events that cannot bubble are: 1. focus event; 2. blur event; 3. scroll event; 4. mouseenter and mouseleave events; 5. mouseover and mouseout events; 6. mousemove event; 7. keypress event; 8. beforeunload event ; 9. DOMContentLoaded event; 10. cut, copy and paste events, etc.

How to block text messages on iPhone How to block text messages on iPhone Jul 31, 2023 pm 09:49 PM

How to block text messages from someone on iPhone? If you receive a text message from someone you want to block, you need to open the message on your iPhone. Once you've opened the message, click on the icon at the top with the mobile number or sender's name below it. Now click on Messages on the right side of the screen; now you will see another screen with the option to block this caller. Click this button and select Block Contact. The phone number will no longer be able to send you text messages; it will also be blocked from calling your iPhone. How to unblock blocked contacts on iPhone? If you decide to allow a blocked person to send you messages, you can unblock them on your iPhone at any time. To unblock contacts on iPhone, you need to

No support for bubbling events: limitations and scope No support for bubbling events: limitations and scope Jan 13, 2024 pm 12:51 PM

Bubbling event (BubblingEvent) refers to an event delivery method that is triggered step by step from child elements to parent elements in the DOM tree. In most cases, bubbling events are very flexible and scalable, but there are some special cases where events do not support bubbling. 1. Which events do not support bubbling? Although most events support bubbling, there are some events that do not support bubbling. The following are some common events that do not support bubbling: focus and blur events load and unloa

What are the instructions to prevent bubbling events? What are the instructions to prevent bubbling events? Nov 21, 2023 pm 04:14 PM

Instructions to prevent bubbling events include stopPropagation(), cancelBubble attribute, event.stopPropagation(), event.cancelBubble attribute, event.stopImmediatePropagation(), etc. Detailed introduction: 1. stopPropagation() is one of the most commonly used instructions, used to stop the propagation of events. When an event is triggered, calling this method can prevent the event from continuing, etc.

What is the meaning of bubbling events What is the meaning of bubbling events Feb 19, 2024 am 11:53 AM

Bubbling events mean that in web development, when an event is triggered on an element, the event will propagate to upper elements until it reaches the document root element. This propagation method is like a bubble gradually rising from the bottom, so it is called a bubbling event. In actual development, knowing and understanding how bubbling events work is very important to handle events correctly. The following will introduce the concept and usage of bubbling events in detail through specific code examples. First, we create a simple HTML page with a parent element and three children

See all articles