blob: ec7827698217b7c18c4d2b269c99b041e19ae984 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
---
title: document.hasFocus
slug: Web/API/Document/hasFocus
tags:
- API
- DOM
- 参考
- 方法
- 焦点
translation_of: Web/API/Document/hasFocus
---
<p>{{ ApiRef }}</p>
<h3 id="Summary" name="Summary">概述</h3>
<p><code><strong>Document.hasFocus()</strong></code> 方法返回一个 {{jsxref("Boolean")}},表明当前文档或者当前文档内的节点是否获得了焦点。该方法可以用来判断当前文档中的活动元素是否获得了焦点。</p>
<div class="note">当查看一个文档时,当前文档中获得焦点的元素一定是当前文档的活动元素,但一个文档中的活动元素不一定获得了焦点.。例如, 一个在后台的窗口中的活动元素一定没有获得焦点。</div>
<h3 id="Syntax" name="Syntax">语法</h3>
<pre class="eval">focused = document.hasFocus();
</pre>
<h4 id="返回值">返回值</h4>
<p>如果当前文档的活动元素获得了焦点,返回<code> true</code>,否则返回false。</p>
<h3 id="Example" name="Example">例子</h3>
<pre class="brush: js"><!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 ("<a href="http://developer.mozilla.org/">http://developer.mozilla.org/</a>", "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>
</pre>
<h3 id="浏览器兼容性"><span>浏览器兼容性</span></h3>
{{Compat("api.Document.hasFocus")}}
|