HTMLSelectElement: add()-Methode - Web-APIs | MDN
Baseline
Weitgehend verfügbar
Diese Funktion ist gut etabliert und funktioniert auf vielen Geräten und in vielen Browserversionen. Sie ist seit Juli 2015 browserübergreifend verfügbar.
Die HTMLSelectElement.add()-Methode fügt ein Element zur Sammlung der option-Elemente für dieses select-Element hinzu.
Syntax
js
add(item)
add(item, before)
Parameter
item-
Ein
HTMLOptionElementoderHTMLOptGroupElement beforeOptional-
Ein Element der Sammlung oder ein Index vom Typ long, vor welchem der item eingefügt werden soll. Wenn dieser Parameter
nullist (oder der Index nicht existiert), wird das neue Element am Ende der Sammlung angehängt.
Rückgabewert
Keiner (undefined).
Ausnahmen
HierarchyRequestErrorDOMException-
Ausgelöst, wenn das item, das an die Methode übergeben wird, ein Vorfahre des
HTMLSelectElementist.
Beispiele
Elemente von Grund auf neu erstellen
js
const sel = document.createElement("select");
const opt1 = document.createElement("option");
const opt2 = document.createElement("option");
opt1.value = "1";
opt1.text = "Option: Value 1";
opt2.value = "2";
opt2.text = "Option: Value 2";
sel.add(opt1, null);
sel.add(opt2, null);
/*
Produces the following, conceptually:
<select>
<option value="1">Option: Value 1</option>
<option value="2">Option: Value 2</option>
</select>
*/
Der before-Parameter ist optional. Daher ist Folgendes akzeptiert.
js
sel.add(opt1);
sel.add(opt2);
An eine bestehende Sammlung anhängen
js
const sel = document.getElementById("existingList");
const opt = document.createElement("option");
opt.value = "3";
opt.text = "Option: Value 3";
sel.add(opt, null);
/*
Takes the existing following select object:
<select id="existingList">
<option value="1">Option: Value 1</option>
<option value="2">Option: Value 2</option>
</select>
And changes it to:
<select id="existingList">
<option value="1">Option: Value 1</option>
<option value="2">Option: Value 2</option>
<option value="3">Option: Value 3</option>
</select>
*/
Der before-Parameter ist optional. Daher ist Folgendes akzeptiert.
In eine bestehende Sammlung einfügen
js
const sel = document.getElementById("existingList");
const opt = document.createElement("option");
opt.value = "3";
opt.text = "Option: Value 3";
sel.add(opt, sel.options[1]);
/*
Takes the existing following select object:
<select id="existingList">
<option value="1">Option: Value 1</option>
<option value="2">Option: Value 2</option>
</select>
And changes it to:
<select id="existingList">
<option value="1">Option: Value 1</option>
<option value="3">Option: Value 3</option>
<option value="2">Option: Value 2</option>
</select>
*/
Spezifikationen
| Spezifikation |
|---|
| HTML # dom-select-add-dev |
Browser-Kompatibilität
Help improve MDN
Erfahren Sie, wie Sie beitragen können Diese Seite wurde automatisch aus dem Englischen übersetzt.