JavaScript/jQuery:测试窗口是否有焦点 [英] JavaScript / jQuery: Test if window has focus
本文介绍了JavaScript/jQuery:测试窗口是否有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试浏览器是否有焦点?
How do you test if the browser has focus?
推荐答案
我没有在其他浏览器中测试过这个,但它似乎在 Webkit 中工作.我让你试试IE.:o)
I haven't tested this in other browsers, but it seems to work in Webkit. I'll let you try IE. :o)
试试看: http://jsfiddle.net/ScKbk/
单击开始间隔后,更改浏览器窗口的焦点以查看结果变化.同样,仅在 Webkit 中进行了测试.
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);
});
这篇关于JavaScript/jQuery:测试窗口是否有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文