From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/document/hasfocus/index.html | 122 +++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 files/zh-cn/web/api/document/hasfocus/index.html (limited to 'files/zh-cn/web/api/document/hasfocus/index.html') diff --git a/files/zh-cn/web/api/document/hasfocus/index.html b/files/zh-cn/web/api/document/hasfocus/index.html new file mode 100644 index 0000000000..e7386bd4d1 --- /dev/null +++ b/files/zh-cn/web/api/document/hasfocus/index.html @@ -0,0 +1,122 @@ +--- +title: document.hasFocus +slug: Web/API/Document/hasFocus +tags: + - API + - DOM + - 参考 + - 方法 + - 焦点 +translation_of: Web/API/Document/hasFocus +--- +

{{ ApiRef }}

+ +

概述

+ +

Document.hasFocus() 方法返回一个 {{jsxref("Boolean")}},表明当前文档或者当前文档内的节点是否获得了焦点。该方法可以用来判断当前文档中的活动元素是否获得了焦点。

+ +
当查看一个文档时,当前文档中获得焦点的元素一定是当前文档的活动元素,但一个文档中的活动元素不一定获得了焦点.。例如, 一个在后台的窗口中的活动元素一定没有获得焦点。
+ +

语法

+ +
focused = document.hasFocus();
+
+ +

返回值

+ +

如果当前文档的活动元素获得了焦点,返回 true,否则返回false。

+ +

例子

+ +
<!DOCTYPE html>
+<html>
+<head>
+  <style type='text/css'>
+    #message { font-weight: bold; }
+  </style>
+
+<script type='text/javascript'>
+      setInterval("CheckPageFocus()", 200);
+
+      function CheckPageFocus() {
+            var info = document.getElementById("message");
+           if (document.hasFocus()) {
+             info.innerHTML = "该页面获得了焦点.";
+            }
+            else {
+             info.innerHTML = "该页面没有获得焦点.";
+           }
+      }
+
+    function OpenWindow() {
+           window.open ("http://developer.mozilla.org/", "mozdev",
+                     "width=640, height=300, left=150, top=260");
+    }
+</script>
+</head>
+
+<body>
+ document.hasFocus 演示<br /><br />
+<div id="message">等待用户操作</div><br />
+<button onclick="OpenWindow()">打开一个新窗口</button>
+</body>
+</html>
+
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatGeckoDesktop("1.9") }}6.0{{ CompatNo() }}{{ CompatVersionUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatUnknown() }}{{ CompatGeckoMobile("1.9") }}{{ CompatUnknown() }}{{ CompatNo() }}{{ CompatUnknown() }}
+
+ +

规范

+ + + +

{{ languages( { "en": "en/DOM/document.hasFocus","es": "es/DOM/element.hasFocus", "fr": "fr/DOM/document.hasFocus", "ja": "ja/DOM/document.hasFocus", "pl": "pl/DOM/document.hasFocus" } ) }}

-- cgit v1.2.3-54-g00ecf