JQuery Focus/Blur 和非 Jquery Focus/blur 在 IE 11 (Internet explorer 11) 上不起作用

JQuery Focus/Blur and non Jquery Focus/blur doesn't work on IE 11 (Internet explorer 11)

我想检测我的浏览器 window 是否有焦点(选中)。我使用以下代码来执行此操作:

$(window).focus(function() { 
    window.focusFlag = true; 
}).blur(function() { 
    window.focusFlag = false; 
});

来源: Using JQuery to bind "focus" and "blur" functions for "window", doesn't work in IE

它适用于 mozilla firefox 43.0.4,但不适用于 IE 11。

我也试过focus/blur方法,不涉及JQuery。

function onBlur() {
    document.body.className = 'blurred';
};
function onFocus(){
    document.body.className = 'focused';
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}

它也适用于 mozilla firefox 43.0.4,但不适用于 IE 11。

来源: http://www.thefutureoftheweb.com/blog/detect-browser-window-focus

我能对 IE 11 做些什么?

标准为 focus 定义:

This event type is similar to focusin, but is dispatched after focus is shifted, and does not bubble. https://www.w3.org/TR/uievents/#events-focusevent

因此,focusin 将适用于 jQuery 中的 parents。

<input type="text" />

$(window).focusin(function() { alert("Focussed"); }).focusout(function() { alert("Blur"); });

Try in JSFiddle