HTML DOM add() 方法
定義和用法
add() 方法用于向 <select> 添加一個(gè) <option> 元素。
語法
selectObject.add(option,before)
參數(shù) | 描述 |
---|---|
option | 必需。要添加選項(xiàng)元素。必需是 option 或 optgroup 元素。 |
before | 必需。在選項(xiàng)數(shù)組的該元素之前增加新的元素。如果該參數(shù)是null,元素添加到選項(xiàng)數(shù)組的末尾。 |
實(shí)例
下面的例子可向下拉列表的末尾添加一個(gè) "kiwi" 選項(xiàng):
<html> <head> <script type="text/javascript"> function insertOption() { var y=document.createElement('option'); y.text='Kiwi' var x=document.getElementById("mySelect"); try {x.add(y,null);
// standards compliant } catch(ex) {x.add(y);
// IE only } } </script> </head> <body> <form> <select id="mySelect"> <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option> </select> <input type="button" onclick="insertOption()" value="Insert option" /> </form> </body> </html>