简体   繁体   中英

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. To prevent this I have the following code, and it works perfectly in Chrome and even Edge. Why is this not working in 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

Firefox is having some troubles with activeElement, try this code below:

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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