remove() 方法用于从下拉列表删除选项。 语法 selectObject.remove(index) index -- 必需:规定要删除的选项的索引号。 说明 该方法从选项数组的指定位置移除 <option> 元素。如果指定的下标比 0 小,或者大于或等于选项的数目,remove() 方法会忽略它并什么也不做。 下面的例子可从列表中删除被选的选项: <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> 注意:在删除大量node时,循环删除注意倒着删除,不要从小向大删,否则会出现删除不干净的情况.
千万不要这样删除 var re = document.getElementsByClassName('remove'); for (var i = 0;i <re.length;i++) { re[i].remove(); console.log(i); } 会出现删不干净的问题 |