diff options
Diffstat (limited to 'files/ja/web/api/element/focus_event/index.md')
-rw-r--r-- | files/ja/web/api/element/focus_event/index.md | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/files/ja/web/api/element/focus_event/index.md b/files/ja/web/api/element/focus_event/index.md new file mode 100644 index 0000000000..c087c896b0 --- /dev/null +++ b/files/ja/web/api/element/focus_event/index.md @@ -0,0 +1,137 @@ +--- +title: 'Element: focus イベント' +slug: Web/API/Element/focus_event +tags: + - API + - DOM + - Element + - Event + - Focus + - FocusEvent + - Reference + - イベント +translation_of: Web/API/Element/focus_event +--- +<div>{{APIRef}}</div> + +<p><strong><code>focus</code></strong> イベントは、要素がフォーカスを受け取ったときに発生します。このイベントと {{domxref("Element/focusin_event", "focusin")}} との違いは、 <code>focusin</code> がバブリングを行うのに対し <code>focus</code> は行わないことです。</p> + +<p><code>focus</code> の反対は {{domxref("Element/blur_event", "blur")}} です。</p> + +<table class="properties"> + <tbody> + <tr> + <th scope="row">バブリング</th> + <td>なし</td> + </tr> + <tr> + <th scope="row">キャンセル可能</th> + <td>いいえ</td> + </tr> + <tr> + <th scope="row">インターフェイス</th> + <td>{{DOMxRef("FocusEvent")}}</td> + </tr> + <tr> + <th scope="row">イベントハンドラープロパティ</th> + <td>{{domxref("GlobalEventHandlers/onfocus", "onfocus")}}</td> + </tr> + <tr> + <th scope="row">同期 / 非同期</th> + <td>同期</td> + </tr> + <tr> + <th scope="row">Composed</th> + <td>はい</td> + </tr> + </tbody> +</table> + +<h2 id="Examples" name="Examples">例</h2> + +<h3 id="Simple_example" name="Simple_example">簡単な例</h3> + +<h4 id="HTML">HTML</h4> + +<pre class="brush: html"><form id="form"> + <input type="text" placeholder="text input"> + <input type="password" placeholder="password"> +</form></pre> + +<h4 id="JavaScript">JavaScript</h4> + +<pre class="brush: js">const password = document.querySelector('input[type="password"]'); + +password.addEventListener('focus', (event) => { + event.target.style.background = 'pink'; +}); + +password.addEventListener('blur', (event) => { + event.target.style.background = ''; +});</pre> + +<h4 id="Result" name="Result">結果</h4> + +<p>{{EmbedLiveSample("Simple_example", '100%', '50px')}}</p> + +<h3 id="Event_delegation" name="Event_delegation">イベント委譲</h3> + +<p>このイベントのイベント委譲を実装する方法は二つあります。 {{Event("focusout")}} イベントを使用するか、 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} の <code>useCapture</code> 引数に <code>true</code> を設定するかです。</p> + +<h4 id="HTML_2">HTML</h4> + +<pre class="brush: html"><form id="form"> + <input type="text" placeholder="text input"> + <input type="password" placeholder="password"> +</form></pre> + +<h4 id="JavaScript_2">JavaScript</h4> + +<pre class="brush: js">const form = document.getElementById('form'); + +form.addEventListener('focus', (event) => { + event.target.style.background = 'pink'; +}, true); + +form.addEventListener('blur', (event) => { + event.target.style.background = ''; +}, true);</pre> + +<h4 id="Result_2" name="Result_2">結果</h4> + +<p>{{EmbedLiveSample("Event_delegation", '100%', '50px')}}</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.Element.focus_event")}}</p> + +<h2 id="See_also" name="See_also">関連情報</h2> + +<ul> + <li>関連イベント: {{domxref("Element/blur_event", "blur")}}, {{domxref("Element/focusin_event", "focusin")}}, {{domxref("Element/focusout_event", "focusout")}}</li> + <li><code>Window</code> を対象としたこのイベント: {{domxref("Window/focus_event", "focus")}} イベント</li> +</ul> |