Option text 属性

定义和用法

text 属性设置或返回 option 元素的文本。

提示:如果没有为 option 元素规定 value 属性,则在提交容器表单时会将文本内容发送到服务器。

另请参阅:

HTML 参考手册:Tag <option> HTML

实例

例子 1

更改下拉列表中 option 元素的文本:

document.getElementById("apple").text = "newTextForApple";

Spróbuj sam

例子 2

返回在下拉列表中所选选项的文本:

var x = selTag.options[selTag.selectedIndex].text;

Spróbuj sam

例子 3

获取下拉列表中所有选项的文本:

var x = document.getElementById("mySelect");
var txt = "All options: ";
var i;
for (i = 0; i < x.length; i++) {
    txt = txt + "\n" + x.options[i].text;
}

Spróbuj sam

Gramatyka

Zwrócenie atrybutu text:

optionObject.text

Ustawienie atrybutu text:

optionObject.text = text

Wartość atrybutu

Wartość Opis
text Określa tekst elementu option.

Szczegółowe informacje techniczne

Zwrócona wartość: Wartość ciągowa, która reprezentuje tekst elementu option.

Wsparcie przeglądarki

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Wsparcie Wsparcie Wsparcie Wsparcie Wsparcie