【问题标题】:JQuery Focus/Blur and non Jquery Focus/blur doesn't work on IE 11 (Internet explorer 11)JQuery Focus/Blur 和非 Jquery Focus/blur 在 IE 11 (Internet explorer 11) 上不起作用
【发布时间】:2016-01-13 13:21:12
【问题描述】:

我想检测我的浏览器窗口是否有焦点(被选中)。我使用以下代码:

$(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。

我也尝试了不涉及 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 做些什么?

【问题讨论】:

标签: javascript jquery html internet-explorer focus


【解决方案1】:

标准为focus定义:

此事件类型类似于 focusin,但在焦点转移后调度,并且不会冒泡。 https://www.w3.org/TR/uievents/#events-focusevent

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

<input type="text" />

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

Try in JSFiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多