gpt4 book ai didi

javascript - chrome 的 document.hasFocus()

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:43 25 4
gpt4 key购买 nike

有人知道用什么代替 Chrome 的 hasFocus() 吗?我想知道我的 Chrome 选项卡何时获得焦点,因此我可以在标题中闪烁一条警告消息。

干杯

最佳答案

您可以监听 onfocus/onblur 事件并以这种方式跟踪页面状态。

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="output">
</div>
<script>
var has_focus = true;

function print(str) {
var out = document.getElementById('output')
out.innerText = out.innerText + "\n" + str;
};

window.onfocus = function() {
print('focus');
has_focus = true;
};

window.onblur = function() {
print('blur');
has_focus = false;
};
</script>
</body>
</html>

关于javascript - chrome 的 document.hasFocus(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2358816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com