HTML-DOM Optionen auswählen Sammlung
Definition und Verwendung
Option, um die Sammlung zurückkehren enthält alle <Option> ist ein Array <select> Element.
Hinweis: Jedes Element im Array entspricht einem <option> -Tag - beginnend bei 0.
Grammatik
selectObject.options
Immobilien
属性 | 描述 |
---|---|
length | 返回集合的option元素数目 |
selectedIndex | 设置或者返回select对象已选选项的索引值。(以 0 起始) |
Weg
方法 | 描述 |
---|---|
[index] | 以数字形式指定元素索引 (以 0 开始) |
[add(element[,index])] | 在集合中添加option元素 |
item(index) | 以数字索引返回集合中元素 |
namedItem(name) | 以名称为索引返回集合元素 |
remove(index) | 从集合中移除元素 |
Browser-Unterstützung
Alle gängigen Browser unterstützen Optionen Sammlung
Beispiele
Beispiele
Zyklus des Ausgangs Dropdown-Liste aller Optionen:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Titel> Dieses Tutorial (w3big.com) </ title>
<Script>
Funktion displayResult () {
var x = document.getElementById ( "MYSELECT");
var txt = "Alle Optionen:";
var i;
for (i = 0; i <x.length; i ++) {
txt = txt + "\ n" + x.options [i] .text;
}
alert (txt);
}
</ Script>
</ Head>
<Body>
<Form>
Ihre Lieblingsfrucht:
<Wählen Sie id = "MYSELECT">
<Option> Apple-</ option>
<Option> Orange </ option>
<Option> Ananas </ option>
<Option> Banana </ option>
</ Select>
</ Form>
<Taste type = "button" Onclick = "displayResult ()"> den Text aller Optionen anzuzeigen </ button>
</ Body>
</ Html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Titel> Dieses Tutorial (w3big.com) </ title>
<Script>
Funktion displayResult () {
var x = document.getElementById ( "MYSELECT");
var txt = "Alle Optionen:";
var i;
for (i = 0; i <x.length; i ++) {
txt = txt + "\ n" + x.options [i] .text;
}
alert (txt);
}
</ Script>
</ Head>
<Body>
<Form>
Ihre Lieblingsfrucht:
<Wählen Sie id = "MYSELECT">
<Option> Apple-</ option>
<Option> Orange </ option>
<Option> Ananas </ option>
<Option> Banana </ option>
</ Select>
</ Form>
<Taste type = "button" Onclick = "displayResult ()"> den Text aller Optionen anzuzeigen </ button>
</ Body>
</ Html>
Versuchen »
Weitere Beispiele
Auch durch einen Pull-Down - Menü - Optionen zu ändern Optionen für den aktuellen Drop-Down - Menü.
Objekte auswählen