此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

HTMLSelectElement:remove() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

HTMLSelectElement.remove() 方法会从 select 元素的 options 集合中移除指定索引处的元素。

语法

js
remove(index)

参数

index

一个从零开始的整数,表示要从集合中移除的 HTMLOptionElement 的索引。如果未找到该索引,则此方法不会产生任何效果。

返回值

无(undefined)。

示例

html
<select id="existingList" name="existingList">
  <option value="1">选项:值 1</option>
  <option value="2">选项:值 2</option>
  <option value="3">选项:值 3</option>
</select>
js
let sel = document.getElementById("existingList");
sel.remove(1);

现在的 HTML 是:

html
<select id="existingList" name="existingList">
  <option value="1">选项:值 1</option>
  <option value="3">选项:值 3</option>
</select>

规范

Specification
HTML
# dom-select-remove

浏览器兼容性

参见