aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/window/focus_event/index.md
blob: 75d432508ba0077357301283925eba6bbdf4900f (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
---
title: 'Window: focus イベント'
slug: Web/API/Window/focus_event
tags:
  - API
  - イベント
  - Focus
  - FocusEvent
  - リファレンス
  - ウェブ
  - Window
  - onfocus
browser-compat: api.Window.focus_event
translation_of: Web/API/Window/focus_event
---
{{APIRef}}

**`focus`** イベントは、要素がフォーカスを受け取ったときに発生します。

`focus` の反対は {{domxref("Window/blur_event", "blur")}} です。

<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>

## 例

### ライブデモ

この例ではフォーカスを失ったときに文書の外見を変更します。 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} を使用して `focus` および {{domxref("Window/blur_event", "blur")}} イベントを監視します。

#### HTML

```html
<p id="log">Click on this document to give it focus.</p>
```

#### CSS

```css
.paused {
  background: #ddd;
  color: #555;
}
```

#### JavaScript

```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);
```

#### 結果

{{EmbedLiveSample("Live_example")}}

## 仕様書

{{Specifications}}

## ブラウザーの互換性

{{Compat}}

## 関連情報

- 関連イベント: {{domxref("Window/blur_event", "blur")}}
- `Element` を対象としたこのイベント: {{domxref("Element/focus_event", "focus")}} イベント