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

CommandEvent: source プロパティ

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

sourceCommandEvent インターフェイスの読み取り専用プロパティで、指定されたコマンドを呼び出したコントロールを表す EventTarget を返します。

EventTarget オブジェクトです。通常は HTMLButtonElement です。

以下の簡単な例では、CommandEvent が発生した際にボタン要素に一時的なクラスを追加するイベントリスナーを設定しています。

js
document.body.addEventListener(
  "command",
  (event) => {
    const theButton = event.source;

    theButton.classList.add("just-pressed");

    setTimeout(() => {
      theButton.classList.remove("just-pressed");
    }, 1000);
  },
  { capture: true },
);

仕様書

Specification
HTML
# dom-commandevent-source

ブラウザーの互換性

関連情報