代码示例:(标识:jsck_document_hasfocus)
<!DOCTYPE html>
<html>
<body>

<h1>Document 对象</h1>

<h2>hasFocus() 方法</h2>

<p>单击此文档内部可获得焦点。<br>
单击此文档外部可失去焦点。</p>

<p id="demo"></p>

<script>
setInterval("myFunction()", 1);

function myFunction() {
  let text;
  if (document.hasFocus()) {
    text = "本文档获得焦点。";
  } else {
    text = "本文档未获得焦点。";
  }
document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>
运行结果: