Baseline Widely available
Die HTMLSelectElement.add()
-Methode fügt ein Element zur Sammlung der option
-Elemente für dieses select
-Element hinzu.
add(item)
add(item, before)
Parameter
item
Ein HTMLOptionElement
oder HTMLOptGroupElement
before
Optional
Ein Element der Sammlung oder ein Index vom Typ long, vor welchem der item eingefügt werden soll. Wenn dieser Parameter null
ist (oder der Index nicht existiert), wird das neue Element am Ende der Sammlung angehängt.
Keiner (undefined
).
HierarchyRequestError
DOMException
Ausgelöst, wenn das item, das an die Methode übergeben wird, ein Vorfahre des HTMLSelectElement
ist.
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.
sel.add(opt1);
sel.add(opt2);
An eine bestehende Sammlung anhängen
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ügenconst 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 Browser-Kompatibilität MDN-Feedback-Box War diese Ãbersetzung hilfreich?
Diese Seite wurde automatisch aus dem Englischen übersetzt.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4