blob: d000fb54fac22241724d6a7c01f1bd3ef3e86f70 (
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
---
title: 'Window: focus イベント'
slug: Web/API/Window/focus_event
tags:
- API
- Event
- Focus
- FocusEvent
- Reference
- Web
- Window
- onfocus
- イベント
translation_of: Web/API/Window/focus_event
---
<div>{{APIRef}}</div>
<p><strong><code>focus</code></strong> イベントは、要素がフォーカスを取得したときに発生します。</p>
<p><code>focus</code> の反対は {{domxref("Window/blur_event", "blur")}} です。</p>
<table class="properties">
<tbody>
<tr>
<th>バブリング</th>
<td>なし</td>
</tr>
<tr>
<th>キャンセル</th>
<td>不可</td>
</tr>
<tr>
<th>インターフェイス</th>
<td>{{DOMxRef("FocusEvent")}}</td>
</tr>
<tr>
<th>イベントハンドラープロパティ</th>
<td>{{domxref("GlobalEventHandlers/onfocus", "onfocus")}}</td>
</tr>
<tr>
<th>同期 / 非同期</th>
<td>同期</td>
</tr>
<tr>
<th>Composed</th>
<td>はい</td>
</tr>
</tbody>
</table>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Live_example" name="Live_example">ライブデモ</h3>
<p>この例ではフォーカスを失ったときに文書の外見を変更します。 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} を使用して <code>focus</code> および {{domxref("Window/blur_event", "blur")}} イベントを監視します。</p>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><p id="log">Click on this document to give it focus.</p></pre>
<h4 id="CSS">CSS</h4>
<pre class="brush: css">.paused {
background: #ddd;
color: #555;
}</pre>
<h4 id="JavaScript">JavaScript</h4>
<pre class="brush: js">function pause() {
document.body.classList.add('paused');
log.textContent = 'FOCUS LOST!';
}
function play() {
document.body.classList.remove('paused');
log.textContent = 'This document has focus. Click outside the document to lose focus.';
}
const log = document.getElementById('log');
window.addEventListener('blur', pause);
window.addEventListener('focus', play);</pre>
<h4 id="Result" name="Result">結果</h4>
<p>{{EmbedLiveSample("Live_example")}}</p>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
<th scope="col">状態</th>
<th scope="col">備考</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("UI Events", "#event-type-focus")}}</td>
<td>{{Spec2("UI Events")}}</td>
<td>Added info that this event is composed.</td>
</tr>
<tr>
<td>{{SpecName("DOM3 Events", "#event-type-focus")}}</td>
<td>{{Spec2("DOM3 Events")}}</td>
<td>初回定義</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<p>{{Compat("api.Window.focus_event")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>関連イベント: {{domxref("Window/blur_event", "blur")}}</li>
<li><code>Element</code> を対象としたこのイベント: {{domxref("Element/focus_event", "focus")}} イベント</li>
</ul>
|