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 |
瀏覽器相容性
載入中…