HTMLButtonElement: command-Eigenschaft
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimentell: Dies ist eine experimentelle Technologie
Überprüfen Sie die Browser-Kompatibilitätstabelle sorgfältig vor der Verwendung auf produktiven Webseiten.
Die command
-Eigenschaft der HTMLButtonElement
-Schnittstelle ruft die Aktion ab und legt sie fest, die auf einem Element ausgeführt werden soll, das von dieser Schaltfläche gesteuert wird. Damit dies Wirkung zeigt, muss commandfor
gesetzt sein.
Sie spiegelt das command
-HTML-Attribut 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 Events
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
BCD tables only load in the browser