


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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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.

Why don't events bubble up in some cases? Event bubbling means that when an event on an element is triggered, the event will propagate upward from the innermost element until it is passed to the outermost element. But in some cases, the event cannot bubble, that is, the event will only be processed on the triggered element and will not be passed to other elements. This article will introduce some common situations, discuss why events fail to bubble, and provide specific code examples. Use the event capture pattern: Event capture is another way of event delivery, as opposed to event bubbling.

When we were using the win10 system, some friends encountered inexplicable blocking when downloading things. For this situation, the editor thinks it should be a problem with the system firewall. You can try to make relevant settings in the registry to solve the problem, or use the compatibility mode in the properties to open the software, etc. to solve the problem. Let’s take a look at the specific and detailed steps with the editor~ I hope it can help you. What to do if Windows 10 software is blocked from downloading. Method 1: Compatibility Mode 1. Right-click the application, click "Properties" in the menu, select the "Compatibility" tab, and check the "Compatibility Mode" option box. Method 2: Modify security settings. Find "Settings" in the lower left corner, then "Update and Security", and finally "Windows D

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
