您的位置 首页 javascript教程

js中remove怎么用

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 = re.length-1;i >=0;i--) { re[i].remove(); console.log(i); }

千万不要这样删除

var re = document.getElementsByClassName('remove'); for (var i = 0;i <re.length;i++) { re[i].remove(); console.log(i); }

会出现删不干净的问题

以上就是js中remove怎么用的详细内容,更多请关注php中文网其它相关文章!

文章来源:火云技术网

文章标题:js中remove怎么用

文章地址:https://www.pyhw.net/33885_js%e4%b8%adremove%e6%80%8e%e4%b9%88%e7%94%a8.html

关于作者: 火云技术网

热门文章

发表评论

您的电子邮箱地址不会被公开。

网站地图