Select add() 方法
实例
例子 1
在下拉列表末尾添加 "Kiwi" 选项:
var x = document.getElementById("mySelect"); var option = document.createElement("option"); option.text = "Kiwi"; x.add(option);
例子 2
在下拉列表的开头添加 "Kiwi" 选项:
var x = document.getElementById("mySelect"); var option = document.createElement("option"); option.text = "Kiwi"; x.add(option, x[0]);
例子 3
在下拉列表的索引位置 "2" 处添加 "Kiwi" 选项:
var x = document.getElementById("mySelect"); var option = document.createElement("option"); option.text = "Kiwi"; x.add(option, x[2]);
例子 4
在下拉列表中的所选定的选项之前添加一个选项:
var x = document.getElementById("mySelect"); if (x.selectedIndex >= 0) { var option = document.createElement("option"); option.text = "Kiwi"; var sel = x.options[x.selectedIndex]; x.add(option, sel); }
语法
selectObjectadd(option, index)
Parameter value
Parameter | Description |
---|---|
option | Required. Specifies the option to be added. It must be an option or optgroup element. |
index |
Optional. Integer, specifies the index position where the new option element should be inserted. The index starts from 0. If the index is not specified, the new option will be inserted at the end of the list. |
Technical details
Return value:
No return value.
Browser support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |