CommandEvent

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Das CommandEvent-Interface stellt ein Ereignis dar, das den Benutzer benachrichtigt, wenn ein button-Element mit gültigen commandForElement- und command-Attributen ein interaktives Element aufrufen soll.

Dies ist das Ereignisobjekt für das HTMLElement-command-Ereignis, das eine Aktion von einem Invoker Control darstellt, wenn es aufgerufen wird (zum Beispiel, wenn es geklickt oder gedrückt wird).

Event CommandEvent

Konstruktor

CommandEvent()

Erstellt ein CommandEvent-Objekt.

Instanz-Eigenschaften

Dieses Interface erbt Eigenschaften von seinem Elternteil, Event.

CommandEvent.source Schreibgeschützt

Ein HTMLButtonElement, das den Button repräsentiert, der diese Auslösung verursacht hat.

CommandEvent.command Schreibgeschützt

Ein String, der den command-Wert des Quell-Buttons darstellt.

Beispiele

Einfaches Beispiel

html
<button commandfor="mypopover" command="show-popover">Show popover</button>

<div popover id="mypopover" role="[declare appropriate ARIA role]">
  <!-- popover content here -->
  <button commandfor="mypopover" command="hide-popover">Hide popover</button>
</div>
js
const popover = document.getElementById("mypopover");

// ...

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

Benutzerdefiniertes Beispiel

html
<button commandfor="the-image" command="--rotate-left">Rotate Left</button>

<button commandfor="the-image" command="--rotate-right">Rotate Right</button>

<img id="the-image" src="photo.jpg" alt="[add appropriate alt text here]" />
js
const image = document.getElementById("the-image");

image.addEventListener("command", (event) => {
  if (event.command == "--rotate-left") {
    event.target.style.rotate = "-90deg";
  } else if (event.command == "--rotate-right") {
    event.target.style.rotate = "90deg";
  }
});

Spezifikationen

Specification
HTML
# commandevent

Browser-Kompatibilität

Siehe auch