如何测试浏览器是否有焦点?

推荐答案

I haven't tested this in other browsers, but it seems to work in Webkit. I'll let you try IE. :o)

Try it out: http://jsfiddle.net/ScKbk/

After you click to start the interval, change the focus of the browser window to see the result change. Again, tested only in Webkit.

var window_focus;

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

$(document).one('click', function() {
    setInterval(function() {
        $('body').append('has focus? ' + window_focus + '<br>');
    }, 1000);
});​

Jquery相关问答推荐

即使我使用 [FromBody] C# 从视图(Ajax)发送请求时在控制器中收到 NULL 对象

jQuery 或 JavaScript 中的$符号是什么意思?

jQuery:获取数据属性

jQuery.parseJSON 单引号与双引号

.trigger() 与 .click() 中的 jQuery 优势/差异

使用 jQuery 发送 JSON 数据

如何使用jQuery找到元素顶部的垂直距离(以px为单位)

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

如何在 HTML选项标签上显示工具提示?

延期与promise

通过 AJAX 和 jQuery 使用 HTML5 文件上传

datatable jquery - 表头宽度与正文宽度不对齐

jquery: $(window).scrollTop() 但没有 $(window).scrollBottom()

jQuery $(".class").click(); - 多个元素,点击事件一次

判断 JS 对象中是否存在键

使用 jQuery 获取选中复选框的值

如何在纯 JavaScript 中模拟鼠标悬停以激活 CSS:悬停?

单击时平滑滚动到特定的 div

使用 jQuery 在 Select 列表中隐藏选项

如何确定滚动高度?