陪你做好前端设计!
源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>素材8网教程(study.sucai8.cn)</title> </head> <body> <p>点击文档任意一处获取焦点,如果点击文档外则会失去焦点。</p> <p id="demo"></p> <script> setInterval("myFunction()", 1); function myFunction() { var x = document.getElementById("demo"); if (document.hasFocus()) { x.innerHTML = "文档已获取焦点。"; } else { x.innerHTML = "文档失去焦点。"; } } </script> </body> </html>
运行结果