我的代码可以在IE中运行,但在Safari、Firefox和Opera中会崩溃。(惊喜)

document.getElementById("DropList").options.length=0;

经过搜索,我了解到它不喜欢的是长度=0。 我试过了……选项=null和var clear=0...长度=clear,结果相同。

我一次对多个对象这样做,所以我正在寻找一些轻量级的JS代码。


当前回答

使用JQuery是一种更漂亮、更短、更聪明的方法!

$('#selection_box_id').empty();

其他回答

最简单的解决方案是最好的,所以你只需要:

var list = document.getElementById('list'); while (list.firstChild) { list.removeChild (list.firstChild); } <选择id = "列表" > <选项值= " 0 " > 0 < /选项> <选项值= " 1 " > 1 > < /选项 < /选择>

去扭转。原因是每次移除后尺寸都会减小。

for (i = (len-1); i > -1; i--) {
    document.getElementById("elementId").remove(i);
}

这可以用来清除选项:

功能clearDropDown () { var选择=文档。getElementById(“DropList”), 选择。 当(长度正好…){ 选择。移除(长度正好); 的 的 <选择id=“DropList”> <选项> option_1 < /选项> <选项> option_2 < /选项> <选项> option_3 < /选项> <选项> option_4 < /选项> <选项> option_5 < /选项> 选择< - > <按钮onclick =“clearDropDown ()> revival巴顿list < - >

这是一种现代的纯JavaScript

document.querySelectorAll('#selectId option').forEach(option => option.remove())
while(document.getElementById("DropList").childNodes.length>0) 
{
    document.getElementById("DropList").removeChild(document.getElementById("DropList").childNodes[0]);
}