HTMLElement: command 事件
HTMLElement
接口的 command
事件,会在一个由带有有效 commandForElement
和 command
值的 HTMLElement
(例如,一个 button
元素)所控制的元素上触发,当用户与该按钮交互(例如,点击它)时。
语法
在诸如 addEventListener()
之类的方法中使用事件名称,或设置事件处理程序属性。
js
addEventListener("command", (event) => { })
oncommand = (event) => { }
事件类型
一个 CommandEvent
。继承自 Event
。
示例
基本示例
js
const popover = document.getElementById("mypopover");
// …
popover.addEventListener("command", (event) => {
if (event.action === "show-popover") {
console.log("Popover is about to be shown");
}
});
事件分发和取消
值得注意的是,command
事件会在被调用的元素上触发。如果按钮被点击,它会首先触发一个 click
事件,如果该事件被取消,那么 command
事件就不会触发,并且默认行为也不会被执行。除了取消按钮上的 click
事件外,还可以取消 command
事件。
例如
js
button.addEventListener("click", (event) => {
event.preventDefault(); // the `command` event will never fire
});
js
element.addEventListener("command", (event) => {
event.preventDefault(); // the `command` event fires but the default behavior is cancelled
});
规范
规范 |
---|
HTML # event-command |
浏览器兼容性
加载中…