


js uses the event to prevent bubbling to hide the click blank modal box_javascript skills
Many times, when we are working on the front end, we will have such a small function. Click to pop up a certain box. However, sometimes we don’t want to operate, so we want to click on a blank space to hide the box. Assume the following scenario, a small button can pop up a modal box when clicked.
It’s that simple, but we want to hide the modal box when clicking on the blank part, add button id: btn, modal box id: model
Perhaps our simplest idea is to directly Listen for an event on the document. The pseudo code is as follows:
Button click pop-up event monitoring:
$("#btn").bind("click",function(e){
if(e.stopPropagation){//Need to prevent bubbling
e .stopPropagation();
}else{
e.cancelBubble = true;
}
})
$(document).bind("click",function(e){
if (the click event is not on the model) {
Hide modal box;
}
})
At first glance, this is no problem, but in fact, there are many problems. First, we have to pay attention to blocking Bubble, otherwise, clicking the button actually triggers the above two events. The modal cannot pop up. In fact, it pops up and is hidden immediately. Moreover, when we have many small controls on the modal box, Sometimes, it is difficult for us to judge the click in the modal box.
Here, I think there is one of the most classic methods, which is very simple but very clever.
First, listen for an event for the modal box as follows:
$("#modal").bind("click", function(event) {
if (event && event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
});
Simply prevent click events from bubbling up in the modal,
Then:
$(document).one("click", function(e){
var $target = $(e.currentTarget);
if ( $target.attr("id") != "modal") {
$("#modal").css("display", "none");
}
});
Done, so easy

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

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.

How to use Vue to implement modal box special effects With the development of Internet technology, modal boxes (Modal) are widely used in web design as a common interaction method. Modal boxes can be used to display pop-up windows, warnings, confirmations and other information to give users a better interactive experience. This article will introduce how to use the Vue framework to implement a simple modal box effect and provide specific code examples. The following are the steps to implement modal box effects: Create a Vue instance. First, we need to introduce the Vue CDN link in the HTML file and add it in Ja

HTML, CSS and jQuery: Build a beautiful modal box Introduction: Pop-up windows or modal boxes are often used in web pages to display information and achieve interactive effects. This article will introduce how to use HTML, CSS and jQuery to build a beautiful modal box, with specific code examples. 1. HTML structure: First, we need to create an HTML structure to accommodate the modal box. The code looks like this: <!DOCTYPEhtml><html>

How to implement dialog boxes and modal boxes in Vue? With the continuous development and updating of front-end technology, the development of front-end pages has become more and more complex and diverse. Dialog boxes and modal boxes are elements that often appear in front-end pages and can help us achieve more flexible and diverse interactive effects. In Vue, there are many ways to implement dialog boxes and modal boxes. This article will introduce you to several common implementation methods. 1. Use Vue’s own components. Vue.js provides some built-in components, such as transition and tra.

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:

Master the command skills to stop bubbling events! When we use electronic devices, we often encounter triggers from various events. Some events are like bubbles, popping up from one place and then spreading to other places, affecting our normal operations. In order to avoid the spread of bubbling events, we need to master some command skills. This article will introduce some common command techniques to prevent bubbling events to help readers better deal with such problems. First, let's understand what a bubbling event is. In computer programming, a bubbling event occurs when an element triggers something

Vue component development: Modal box component implementation method In web applications, the modal box is a common UI control that can be used to display some important content, such as prompt information, warning information, prompting the user to perform certain operations, etc. . This article will introduce how to use the Vue framework to develop a simple modal box component and provide code examples for reference. Component structure First we need to define a modal box component, including HTML structure, style and logical functions. Components usually have a parent component passing properties to child components, and child components pass properties based on the properties.

How to implement modal box function using JavaScript? In web development, modal boxes are a common interactive component that can be used to pop up prompts, confirmation boxes, or display detailed content. This article will introduce how to use JavaScript to implement a simple modal box and give specific code examples. 1. HTML structure First, we need to add the structure of the modal box to HTML. You can use a div element as the container of the modal box and add the tag to the div.
