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

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

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

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


当前回答

使用PrototypeJS:

$('yourSelect').select('option').invoke('remove');

其他回答

function removeOptions(obj) {
    while (obj.options.length) {
        obj.remove(0);
    }
}

也许,这不是最干净的解决方案,但绝对比一个一个地移除简单:

document.getElementById("DropList").innerHTML = "";

今天我也遇到了同样的问题,我在重新加载选择框时做了如下操作。(在纯JS中)

        var select = document.getElementById("item");
        select.options.length = 0;
        var opt = document.createElement('option');
        opt.value = 0;
        opt.innerHTML = "Select Item ...";
        opt.selected = "selected";
        select.appendChild(opt);


       for (var key in lands) {
            var opt = document.createElement('option');
            opt.value = lands[key].id;
            opt.innerHTML = lands[key].surveyNo;
            select.appendChild(opt);

        }

使用PrototypeJS:

$('yourSelect').select('option').invoke('remove');

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

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