javascript JQuery $(window).focusout 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5143464/
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
JQuery $(window).focusout issue
提问by Uriel Bertoche
the problem is, I'm making a program like a chat, that needs to know if the user has left the window, or changed to another window or tab, to allow other users see that the other user is not seeing the page right now.
问题是,我正在制作一个像聊天这样的程序,它需要知道用户是否离开了窗口,或者切换到另一个窗口或选项卡,以允许其他用户看到其他用户现在没有看到该页面.
I thought the window event focusout would solve my problems, however, there are a few issues with it. First: it does not fire only when the user leaves the window, if they focus on a input field then click anywhere else in the page, the event fires. Obviously that's intolerable. I managed, in Firefox, a way around that. On Firefox, when that happens, the browser fires the focusout event once. If you really leave the window however, it fires it twice. So, a little programming made the magic.
我认为 window event focusout 会解决我的问题,但是,它存在一些问题。第一:它不会仅在用户离开窗口时触发,如果他们专注于输入字段然后单击页面中的任何其他地方,事件就会触发。显然这是不能容忍的。我在 Firefox 中设法解决了这个问题。在 Firefox 上,当这种情况发生时,浏览器会触发一次 focusout 事件。然而,如果你真的离开窗口,它会触发两次。所以,一点编程创造了魔力。
Then came the second problem: Chrome, and I believe other browser might behave the same, only fires focusout event once, no matter what you do. Leaving the window, changing focus from inputs to page, it's the same, so, my programming didn't worked there.
然后是第二个问题:Chrome,我相信其他浏览器的行为可能相同,无论您做什么,都只会触发一次 focusout 事件。离开窗口,将焦点从输入更改为页面,这是相同的,因此,我的编程在那里不起作用。
Does anyone know a way to simulate the desired behavior? Or a way to make Chrome and other possible browser to behave like Firefox or whatever?
有谁知道一种模拟所需行为的方法?或者让 Chrome 和其他可能的浏览器表现得像 Firefox 或其他什么的方法?
Thanks everyone!
感谢大家!
回答by ?ime Vidas
Use blurinstead of focusout:
使用blur代替focusout:
$(window).blur(function() {
// code
});
The difference is that focusoutbubbles up the DOM tree and blurdoesn't. If you set focusouton the window object, then all the blur events that occur on page elements will also trigger that handler which is something that you don't want.
不同之处在于focusoutDOM 树会冒泡,而blur不会。如果focusout在 window 对象上设置,则页面元素上发生的所有模糊事件也将触发该处理程序,这是您不想要的。
The difference is that focusout(when set on the window object) captures all blur events that fire on the page (at any element). In contrast blur(when set on the window object) does not capture those blur events.
不同之处在于focusout(在 window 对象上设置时)捕获在页面上(在任何元素处)触发的所有模糊事件。相比之下blur(当设置在 window 对象上时)不会捕获那些模糊事件。
Live demo:http://jsfiddle.net/simevidas/taRG6/
回答by jenson-button-event
Does this answer it? Talks about using blur event
这能回答吗?浅谈使用模糊事件
回答by Rémy THEROUX
I answer the question, this solution works perfectly except that i catch the event twice.
我回答了这个问题,这个解决方案完美无缺,只是我捕获了两次事件。
BLURevent on windowis fired only when i click another brower tab or when i switch to another application.
仅当我单击另一个浏览器选项卡或切换到另一个应用程序时,才会触发窗口上的BLUR事件。

