


Js bubbling event detailed explanation and blocking example_javascript skills
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:
<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>
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:
$('#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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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 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

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 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

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

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.

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
