简体   繁体   English

jQuery .blur在Firefox中不起作用

[英]jQuery .blur not working in Firefox

I'm trying to hide my website when it loses focus. 当网站失去焦点时,我试图隐藏我的网站。 The problem is that it loses focus when I click inside an iframe on the site. 问题是,当我在网站上的iframe中单击时,它失去了焦点。 To prevent this I have the following code, and it works perfectly in Chrome and even Edge. 为避免这种情况,我使用了以下代码,它在Chrome甚至Edge中都可以完美运行。 Why is this not working in Firefox? 为什么这在Firefox中不起作用?

jQuery(window).focus(function() {
    jQuery("body").show();
}).blur(function() {
    if(document.activeElement != (document.getElementsByTagName("iframe")[0] || document.getElementsByTagName("embed")[0])) {
        jQuery("body").hide();
    }
});

jQuery(document).ready doesn't solve my problem jQuery(document).ready不能解决我的问题

Firefox is having some troubles with activeElement, try this code below: Firefox使用activeElement时遇到了一些麻烦,请尝试以下代码:

jQuery(window).focus(function(e) {
    jQuery("body").show();
}).blur(function(e) {
    if(e.target != (document.getElementsByTagName("iframe")[0] || document.getElementsByTagName("embed")[0])) {
        jQuery("body").hide();
    }
});

Hope it works now. 希望它现在可以工作。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM