How do I make this popup box disappear when I click outside?(当我在外面点击时,如何让这个弹出框消失?)
问题描述
http://jsfiddle.net/mnbayazit/by3zy/2/
当我单击背景上的某个位置时,我希望弹出窗口消失.问题是,当我单击 [X] 或弹出窗口本身时,它会消失.
I want the popup to disappear when I click somewhere on the background. Problem is, it disappears when I click an [X] or the popup itself.
如果这让我的意图更清楚,想象一下它是一个日历选择器.
Imagine it being a calendar-picker if that makes my intentions more clear.
我怎样才能让它做到这一点?
How can I get it to do that?
推荐答案
为主体设置一个
click
处理程序以删除您的弹出窗口.
Set a
click
handler for the body to remove your popup.
为弹出窗口本身设置一个 click
处理程序,该处理程序在事件上调用 stopPropagation()
,以防止它冒泡到正文.
Set a click
handler for the popup itself that calls stopPropagation()
on the event, to prevent it from bubbling up to the body.
大致:
function showMyPopup(){
...
$(myPopupDiv).click(function(e){
e.stopPropagation();
});
}
function closeMyPopup(){
...
}
$(document.body).click(closeMyPopup);
这篇关于当我在外面点击时,如何让这个弹出框消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我在外面点击时,如何让这个弹出框消失?


基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01