此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

CommandEvent:source 属性

Baseline 2025
Newly available

Since ⁨December 2025⁩, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

CommandEvent 接口的只读属性 source 返回一个表示触发该命令的控件的 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

浏览器兼容性

参见