HTMLSelectElement.remove()

HTMLSelectElement.remove() 方法从一个 select 元素中删除指定序数的 option 元素。没有传参时为删除当前元素本身:ChildNode.remove()

语法

collection.remove(index);

参数

  • index 是从 option HTMLOptionElement 集合中要移除元素的序数。如果序数对应的元素不存在,调用这个方法就没有任何效果。

例子

var sel = document.getElementById("existingList");
sel.remove(1);

/*
  上面的代码会将下面的 select 元素结构:

  <select id="existingList" name="existingList">
    <option value="1">Option: Value 1</option>
    <option value="2">Option: Value 2</option>
    <option value="3">Option: Value 3</option>
  </select>

  变成这样:

  <select id="existingList" name="existingList">
    <option value="1">Option: Value 1</option>
    <option value="3">Option: Value 3</option>
  </select>
*/

Specifications

Specification Status Comment
HTML Living Standard
HTMLSelectElement.remove()
Living Standard
HTML5
HTMLSelectElement.remove()
Recommendation Is a snapshot of HTML Living Standard.
Document Object Model (DOM) Level 2 HTML Specification
HTMLSelectElement.remove()
Obsolete
Document Object Model (DOM) Level 1 Specification
HTMLSelectElement.remove()
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
removeChrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

参考