Invoker Commands API
CommandEvent 接口的只读属性 source 返回一个表示触发该命令的控件的 EventTarget。
值
EventTarget 对象。通常是一个 HTMLButtonElement。
示例
在下面的简单示例中,我们设置了一个事件监听器,在发生 CommandEvent 时向按钮元素添加一个临时类:
document.body.addEventListener(
"command",
(event) => {
const theButton = event.source;
theButton.classList.add("just-pressed");
setTimeout(() => {
theButton.classList.remove("just-pressed");
}, 1000);
},
{ capture: true },
);
规范
浏览器兼容性
参见
- Invoker Commands API
- HTMLButtonElement.command
- HTMLButtonElement.commandForElement
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
