From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../ja/web/api/htmlelement/change_event/index.html | 147 +++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 files/ja/web/api/htmlelement/change_event/index.html (limited to 'files/ja/web/api/htmlelement/change_event/index.html') diff --git a/files/ja/web/api/htmlelement/change_event/index.html b/files/ja/web/api/htmlelement/change_event/index.html new file mode 100644 index 0000000000..2449327908 --- /dev/null +++ b/files/ja/web/api/htmlelement/change_event/index.html @@ -0,0 +1,147 @@ +--- +title: 'HTMLElement: change イベント' +slug: Web/API/HTMLElement/change_event +tags: + - Change + - Event + - HTML + - HTML DOM + - HTMLElement + - Reference + - Web +translation_of: Web/API/HTMLElement/change_event +--- +
{{APIRef}}
+ +

change イベントは {{HTMLElement("input")}}, {{HTMLElement("select")}}, {{HTMLElement("textarea")}} 要素において、ユーザーによる要素の値の変更が確定したときに発生します。 {{domxref("HTMLElement/input_event", "input")}} イベントとは異なり、 change イベントは要素の値 (value) が変更されるたびに発生するとは限りません。

+ + + + + + + + + + + + + + + + + + + + +
バブリングあり
キャンセル不可
インターフェイス{{domxref("Event")}}
イベントハンドラープロパティ{{domxref("GlobalEventHandlers/onchange", "onchange")}}
+ +

変更される要素の種類やユーザーが要素を操作する方法によって、 change イベントは異なる時点で発生します。

+ + + +

HTML 仕様書には、 change イベントを発生させる <input>の一覧があります。

+ +

+ +

<select> 要素

+ +
+

HTML

+ +
<label>アイスクリームの味:
+  <select class="ice-cream" name="ice-cream">
+    <option value="">1つ選択してください …</option>
+    <option value="chocolate">チョコレート</option>
+    <option value="sardine">イワシ</option>
+    <option value="vanilla">バニラ</option>
+  </select>
+</label>
+
+<div class="result"></div>
+ + + +

JavaScript

+ +
const selectElement = document.querySelector('.ice-cream');
+
+selectElement.addEventListener('change', (event) => {
+  const result = document.querySelector('.result');
+  result.textContent = `You like ${event.target.value}`;
+});
+
+
+ +

結果

+ +

{{ EmbedLiveSample('select-example', '100%', '75px') }}

+ +

テキスト入力要素

+ +

<input type="text"> など一部の要素では、コントロールがフォーカスを失うまで change イベントが発生しません。以下のフィールドに何かを入力してから、他の部分をクリックするとイベントが発生します。

+ +

HTML

+ +
<input placeholder="何かテキストを入力" name="name"/>
+<p id="log"></p>
+ +

JavaScript

+ +
const input = document.querySelector('input');
+const log = document.getElementById('log');
+
+input.addEventListener('change', updateValue);
+
+function updateValue(e) {
+  log.textContent = e.srcElement.value;
+}
+ +

結果

+ +

{{ EmbedLiveSample('Text_input_element', '100%', '75px') }}

+ +

仕様書

+ + + + + + + + + + + + + + +
仕様書状態
{{SpecName('HTML WHATWG', "indices.html#event-change", "change")}}{{Spec2('HTML WHATWG')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("api.HTMLElement.change_event")}}

+ +

すべてのブラウザーにおいて、特定の操作で change イベントが発生するかどうかが同じであるとは限りません。例えば、 Gecko では {{HTMLElement("select")}} 要素をキーボードで操作すると、 change イベントは Enter を押すか <select> からフォーカスが離れるまで発生しませんでした ({{bug("126379")}} を参照)。ただし、 Firefox 63 (Quantum) からは、すべての主要なブラウザーと同じ動作になりました。

+ +

関連情報

-- cgit v1.2.3-54-g00ecf