Document: selectionchange イベント
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2017年3月以降、すべてのブラウザーで利用可能です。
selectionchange イベントは Selection API の一部で、文書における現在のテキストの選択が変更された際に発生します。
このイベントはキャンセル不可で、バブリングしません。
このイベントは selectionchange のイベントリスナーを追加するか、onselectionchange イベントハンドラーを使用して処理することができます。
メモ:
このイベントは <input> や <textarea> 要素のテキスト選択が変更されたときに発行される selectionchange イベントとは異なります。詳しくはHTMLInputElement.selectionchange_eventを参照してください。
構文
このイベント名を addEventListener() などのメソッドで使用するか、イベントハンドラープロパティを設定するかしてください。
js
addEventListener("selectionchange", (event) => {});
onselectionchange = (event) => {};
イベント型
一般的な Event です。
例
js
// addEventListener 版
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
// onselectionchange 版
document.onselectionchange = () => {
console.log(document.getSelection());
};
仕様書
| 仕様書 |
|---|
| Selection API> # selectionchange-event> |
| Selection API> # dom-globaleventhandlers-onselectionchange> |