📅  最后修改于: 2023-12-03 14:52:46.705000             🧑  作者: Mango
在Javascript中,我们可以通过document.hasFocus()
函数来判断当前窗口是否处于焦点状态。下面是具体步骤:
window.addEventListener('blur', function(event) {
console.log('Window lost focus!');
});
上述代码会在窗口失去焦点时打印一条信息。
window.addEventListener('focus', function(event) {
console.log('Window has focus!');
});
上述代码会在窗口获得焦点时打印一条信息。
if(document.hasFocus()){
console.log('Window is focused!');
} else {
console.log('Window is not focused!');
}
上述代码会根据当前窗口的焦点状态打印不同的信息。
完整代码演示:
window.addEventListener('blur', function(event) {
console.log('Window lost focus!');
});
window.addEventListener('focus', function(event) {
console.log('Window has focus!');
});
if(document.hasFocus()){
console.log('Window is focused!');
} else {
console.log('Window is not focused!');
}
以上就是在Javascript中查看窗口焦点的方法。