Javascript:如何在 Window.Blur () 事件上创建事件侦听器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4349609/
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
Javascript: How to create event listener on Window.Blur () event?
提问by Rella
Javascript: How to create event listener on Window.Blur () event?
Javascript:如何在 Window.Blur () 事件上创建事件侦听器?
回答by user113716
window.onblur = function() {
//say goodbye
};
According to quirksmodeonfocusand onblurare available on the windowin most browsers.
根据 quirksmodeonfocus并且在大多数浏览器中onblur都可用window。
回答by user
It's better to use addEventListenerinstead of a property, this way you can set multiple handlers and nobody (including yourself) will accidentally disable your handler by overwriting the onblurproperty.
最好使用addEventListener而不是属性,这样您就可以设置多个处理程序,并且没有人(包括您自己)会通过覆盖onblur属性来意外禁用您的处理程序。
window.addEventListener('blur', function() {
console.log('blur');
});
回答by Levi Hackwith
Try:
尝试:
window.onBlur = function(e) {
// Code here.
}

