jQuery 单击网站任意位置时如何关闭弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18887878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close popup when click anywhere in website
提问by vivek
I want to close popup ad when user click anywhere in body.
当用户点击正文中的任何地方时,我想关闭弹出广告。
This is my site http://daplonline.in/. I want to hide or close ad when user click anywhere in website.
这是我的网站http://daplonline.in/。当用户点击网站上的任何地方时,我想隐藏或关闭广告。
This is popup html code:
这是弹出的html代码:
<div style="top: 100px; background-color: rgba(5, 5, 0, 0.7); display: block;" id="wd1_nlpopup" data-expires="30" data-delay="10">
<div id="overlay">
<a href="#closepopup" id="wd1_nlpopup_close">x</a>
<div class="content">
<a href="buyonline.php"><img src="images/online_course.gif"/></a>
</div>
</div>
</div>
This is JavaScript code:
这是 JavaScript 代码:
<script type="text/javascript">
$("body").click(function(){
alert("me");
});
</script>
回答by Wiram Rathod
Check this code 100% working and tested.. :)
检查此代码 100% 工作和测试.. :)
$( document ).ready(function() {
$('#wd1_nlpopup_overlay').click(function() {
$('#wd1_nlpopup_overlay').hide();
$('#wd1_nlpopup').hide();
});
});
回答by Dipesh Parmar
You need to edit your selector to below,
你需要编辑你的选择器到下面,
$(function(){
$("#wd1_nlpopup_overlay").click(function(){
alert("me");
});
})
because actually you are clicking on overlay not on body.
因为实际上你点击的是覆盖而不是身体。
Now as this popup might get load later so you need to delegate event handler as below,
现在,由于此弹出窗口可能会稍后加载,因此您需要按如下方式委派事件处理程序,
$(function()
{
$(document).on('click',"#wd1_nlpopup_overlay",function(){
alert("me");
});
})
回答by Jai
What it seems to me to give a click event to the close popup btn. You can do it this way:
在我看来,给关闭弹出窗口 btn 一个点击事件。你可以这样做:
$("#wd1_nlpopup_overlay").click(function(){
$("#wd1_nlpopup_close").click(); // <--this will fire an event to the closebtn
});
回答by Yussuf S
Hi you are forgetting the jQuery document initialiser. try this instead :
您好,您忘记了 jQuery 文档初始化程序。试试这个:
$(document).ready(function() {
$("body").click(function(){
$("#wd1_nlpopup_close").click();
});
});
回答by Prakash Pazhanisamy
You can hide or remove popup using
您可以使用隐藏或删除弹出窗口
$('#your-id').hide();
or
或者
$("#your-id").remove();
回答by Mandeep Jain
try
尝试
$('body').on('click', function(event){
var popup = $('#wd1_nlpopup');
if($(event.target).not(popup)){
$(popup).hide();
}
});
回答by Ashisha Nautiyal
http://jsfiddle.net/jasonday/xpkFf/it will remove popup if will click anywhere
http://jsfiddle.net/jasonday/xpkFf/如果单击任何地方,它将删除弹出窗口
$('#open').click(function() {
$('#dialog').dialog('open');
});
$('#dialog').dialog({
autoOpen: false,
modal: false
});
// Close Pop-in If the user clicks anywhere else on the page
jQuery('html') //set for html for jsfiddle, but should be 'body'
.bind(
'click',
function(e){
if(
jQuery('#dialog').dialog('isOpen')
&& !jQuery(e.target).is('.ui-dialog, a')
&& !jQuery(e.target).closest('.ui-dialog').length
){
jQuery('#dialog').dialog('close');
}
}
);