当焦点离开时如何关闭弹出窗口
要求:将从父窗口打开一个弹出窗口,并且当窗口失去焦点时应将其关闭。 (即使打开另一个应用程序窗口或成为焦点,也会发生这种情况)。
尝试过的代码:
问题: 在弹出窗口主体内单击时,弹出窗口将关闭。
兼容性: ie6及以上、firefox。
我从解决方法 rel="nofollow">http://pro-thoughts.blogspot.com/2006/10/in Correct-behavior-of-windowonblur.html
var active_element;
var bIsMSIE;
function initiateSelfClosing() {
if (navigator.appName == "Microsoft Internet Explorer") {
active_element = document.activeElement;
document.onfocusout = closeWnd;
bIsMSIE = true;
}
else { window.onblur = closeWnd; }
}
function closeWnd() {
if (window.opener != null) {
if (bIsMSIE && (active_element != document.activeElement)) {
active_element = document.activeElement;
}
else {
window.close();
}
}
}
<body onload="initiateSelfClosing()">
</body>
但这里也存在一个问题,如果页面中有打印按钮,并且如果我单击“打印>”然后取消打印作业,弹出窗口将关闭。
有人可以帮我吗...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sathishkumar 的答案中的一些修正
1.我们需要将
win
转换为 jquery 对象2.在 jqvariable 上应用模糊事件,而不是在
window
3.
this.close 在
onblur
事件的定义中已经足够好了Little corrections in sathishkumar's answer
1.we need to convert
win
to jquery object2.Apply blur event on jqvariable and not on
window
3.
this.close
is good enough in definition ofonblur
event使用文档进行模糊事件
Use document for blur event
您不能将 onblur 事件附加到 BODY,但是
您可以在窗口的 Onblur 事件上附加一个函数。
由于您已编辑问题,因此您可以获得更多帮助 这里
You cannot attach onblur event to BODY but
you Can attach a function on Onblur event of window.
Since you have Edited your Question, You can get more help here