HTML-DOM Select Methode add ()
Definition und Verwendung
add () -Methode wird verwendet, um <select> Add <option> Element.
Grammatik
selectObject.add(option,before)
参数 | 描述 |
---|---|
option | 必需。要添加选项元素。必需是 option 或 optgroup 元素。 |
before | 必需。在选项数组的该元素之前增加新的元素。如果该参数是null,元素添加到选项数组的末尾。 |
Browser-Unterstützung
Alle gängigen Browser unterstützen () -Methode hinzufügen
Tipp: add () -Methode erfordert Seite Anweisung in IE8 und späteren Versionen von IE DOCTYPE !.
Beispiele
Beispiele
Das folgende Beispiel kann der Dropdown-Liste an das Ende zu einem "Kiwi" Option hinzugefügt werden:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Titel> Dieses Tutorial (w3big.com) </ title>
<Script>
Funktion displayResult () {
var x = document.getElementById ( "MYSELECT");
var option = document.createElement ( "Option");
option.text = "Kiwi";
try {
// Für frühere Versionen von IE8
x.add (Option, x.options [null]);
} Catch (e) {
x.add (Option, null);
}
}
</ Script>
</ Head>
<Body>
<Form>
<Wählen Sie id = "MYSELECT">
<Option> Apple-</ option>
<Option> Birne </ option>
<Option> Banana </ option>
<Option> Orange </ option>
</ Select>
</ Form>
<br>
<Taste type = "button" Onclick = "displayResult ()"> Einfügen Optionen </ button>
<P> <b> Hinweis: </ b> add () Methode funktioniert in IE8 oder später in Ordnung, eine DOCTYPE-Deklaration in der Seite hinzuzufügen !. Beachten Sie auch, dass die zusätzlichen Code der vorherigen Version von IE 8. </ P>
</ Body>
</ Html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Titel> Dieses Tutorial (w3big.com) </ title>
<Script>
Funktion displayResult () {
var x = document.getElementById ( "MYSELECT");
var option = document.createElement ( "Option");
option.text = "Kiwi";
try {
// Für frühere Versionen von IE8
x.add (Option, x.options [null]);
} Catch (e) {
x.add (Option, null);
}
}
</ Script>
</ Head>
<Body>
<Form>
<Wählen Sie id = "MYSELECT">
<Option> Apple-</ option>
<Option> Birne </ option>
<Option> Banana </ option>
<Option> Orange </ option>
</ Select>
</ Form>
<br>
<Taste type = "button" Onclick = "displayResult ()"> Einfügen Optionen </ button>
<P> <b> Hinweis: </ b> add () Methode funktioniert in IE8 oder später in Ordnung, eine DOCTYPE-Deklaration in der Seite hinzuzufügen !. Beachten Sie auch, dass die zusätzlichen Code der vorherigen Version von IE 8. </ P>
</ Body>
</ Html>
Versuchen »
Weitere Beispiele
Vor der Dropdown-Liste der ausgewählten Option , um Optionen hinzufügen
Objekte auswählen