CommandEvent: thuộc tính 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.

Thuộc tính chỉ đọc source của giao diện CommandEvent trả về một EventTarget biểu diễn điều khiển đã gọi lệnh đã cho.

Giá trị

Một đối tượng EventTarget. Thường là một HTMLButtonElement.

Ví dụ

Trong ví dụ đơn giản sau, chúng ta thiết lập một trình lắng nghe sự kiện để thêm một lớp tạm thời vào phần tử nút khi xảy ra 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 },
);

Thông số kỹ thuật

Thông số kỹ thuật
HTML
# dom-commandevent-source

Khả năng tương thích với trình duyệt

Xem thêm