HTMLSelectElement: remove() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLSelectElement.remove() 方法從該 select 元素的選項集合中移除指定索引處的元素。

語法

js
remove(index)

引數

index

用於指定要從集合中移除的 HTMLOptionElement 的零基索引的整數。如果未找到該索引,則該方法無效。

返回值

無(undefined)。

示例

html
<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>
js
let sel = document.getElementById("existingList");
sel.remove(1);

HTML 現在是

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

規範

規範
HTML
# dom-select-remove

瀏覽器相容性

另見