我有一个选择标签,带有一些选项
<select id="sel">
<option>text1</option>
<option>text2</option>
<option>text3</option>
<option>text4</option>
</select>
我想删除除第二个之外的所有选项,即我想得到
<select id="sel">
<option>text2</option>
</select>
我想它一定是这样的
document.getElementById('sel').options.length= 0;
但它删除了所有列表,所以你能帮助我吗?
<小时/>谢谢
最佳答案
var
sel = document.getElementById("sel"),
options = sel.getElementsByTagName("option");
for (var i=options.length-1; i>=2; i--) {
sel.removeChild(options[i]);
}
sel.removeChild(options[0]);
关于javascript - 如何在javascript中删除除第二个选项之外的所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2850874/