大网数据广告活动
Web 标准与前端开发

HTMLElement: command event

· 52CE 我爱测

The **`command`** event of the HTMLElement interface fires on an element that is controlled via a HTMLButtonElement with valid HTMLButtonElement.commandForElement and HTMLButtonElement.command values, whenever the button is interacted with (e.g., it is clicked).

Invoker Commands API

The command event of the HTMLElement interface fires on an element that is controlled via a HTMLButtonElement with valid HTMLButtonElement.commandForElement and HTMLButtonElement.command values, whenever the button is interacted with (e.g., it is clicked).

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("command", (event) => { })

Alternatively, assign to the event handler property of the interface:

oncommand = (event) => { }

Event type

A CommandEvent. Inherits from Event.

CommandEvent

Examples

Basic example

const popover = document.getElementById("mypopover");

// …

popover.addEventListener("command", (event) => {
  if (event.command === "show-popover") {
    console.log("Popover is about to be shown");
  }
});

Event dispatch and cancellation

It is worth pointing out that command events fire on the element being invoked. If the button is clicked, it will first dispatch a click event which, if canceled, then the command event will not fire and the default behavior will not be run. In addition to canceling the click event on the button, it is also possible to cancel the command event.

For example:

button.addEventListener("click", (event) => {
  event.preventDefault(); // the `command` event will never fire
});
element.addEventListener("command", (event) => {
  event.preventDefault(); // the `command` event fires but the default behavior is canceled
});

Specifications

Browser compatibility

See also

  • Invoker Commands API
  • HTMLButtonElement.command
  • HTMLButtonElement.commandForElement

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。