HTML DOM remove() 方法
定義和用法
remove() 方法用于從下拉列表刪除選項(xiàng)。
語(yǔ)法
selectObject.remove(index)
參數(shù) | 描述 |
---|---|
index | 必需。規(guī)定要?jiǎng)h除的選項(xiàng)的索引號(hào)。 |
說(shuō)明
該方法從選項(xiàng)數(shù)組的指定位置移除 <option> 元素。如果指定的下標(biāo)比 0 小,或者大于或等于選項(xiàng)的數(shù)目,remove() 方法會(huì)忽略它并什么也不做。
實(shí)例
下面的例子可從列表中刪除被選的選項(xiàng):
<html>
<head>
<script type="text/javascript">
function removeOption()
{
var x=document.getElementById("mySelect")
x.remove(x.selectedIndex)
}
</script>
</head>
<body>
<form>
<select id="mySelect">
<option>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>
<input type="button" onclick="removeOption()"
value="Remove option">
</form>
</body>
</html>