HTMLButtonElement: command-Eigenschaft

Limited availability

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

Die command-Eigenschaft der HTMLButtonElement-Schnittstelle erhält und setzt die Aktion, die auf ein Element ausgeführt werden soll, das von diesem Button gesteuert wird. Damit dies eine Wirkung hat, muss commandfor gesetzt sein.

Sie spiegelt das HTML-Attribut command wider.

Wert

Ein String. Siehe das command-Attribut für gültige Werte.

Beispiele

Einfaches Beispiel

html
<button id="toggleBtn" commandfor="mypopover" command="toggle-popover">
  Toggle popover
</button>

<div popover id="mypopover">
  <button commandfor="mypopover" command="hide-popover">Hide popover</button>
</div>
js
const popover = document.getElementById("mypopover");
const toggleBtn = document.getElementById("toggleBtn");

toggleBtn.command = "show-popover";

Benutzerdefiniertes Beispiel, mit Ereignissen

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
# dom-button-command

Browser-Kompatibilität

Siehe auch