JQuery $(窗口).focusout问题 [英] JQuery $(window).focusout issue
问题描述
我认为窗口事件聚焦将解决我的问题,但是,有几个问题。
首先:
只有在用户离开窗口时才会触发,如果他们关注输入字段,然后点击页面的其他任何地方,事件就会触发。显然这是不能容忍的。
我在Firefox中管理一个方法。在Firefox上,当浏览器发生这种情况时,浏览器会触发一次焦点事件。如果你真的离开了窗户,它会发射两次。所以,一个小编程发明了魔法。然后来到第二个问题:
Chrome,我相信其他浏览器可能会表现相同,只会引发焦点事件一次,不管你做什么。离开窗口,把焦点从输入变成页面,这是一样的,所以,我的编程没有在那里工作。
有谁知道一种方法来模拟所需的行为?或者让Chrome浏览器和其他可能的浏览器像Firefox或其他类似的行为?
谢谢大家!
blur
而不是 focusout
: $(window).blur(function(){
// code
});
区别在于 focusout
冒泡DOM树和 blur
不会。如果在窗口对象上设置 focusout
,则页面元素上发生的所有模糊事件也会触发该处理程序,这是您不需要的。 >
区别在于 focusout
(当在窗口对象上设置时)捕获所有在页面(在任何元素)。相比之下, blur
(当在窗口对象上设置的时候)不会捕获那些模糊事件。
$ b 演示: http://jsfiddle.net/simevidas/taRG6/
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.
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.
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?
Thanks everyone!
Use blur
instead of focusout
:
$(window).blur(function() {
// code
});
The difference is that focusout
bubbles up the DOM tree and blur
doesn't. If you set focusout
on 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.
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.
Live demo: http://jsfiddle.net/simevidas/taRG6/
这篇关于JQuery $(窗口).focusout问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!