このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Document: selectionchange イベント

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 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());
};

仕様書

Specification
Selection API
# selectionchange-event
Selection API
# dom-globaleventhandlers-onselectionchange

ブラウザーの互換性

関連情報