[英]Using .remove() on select options (without jQuery)
By mistake I used .remove()
on a option
element and the option
was removed. 错误地,我在
option
元素上使用了.remove()
,并删除了该option
。 That is, the option
I called remove()
on was removed from the select
element. 也就是说,我从
select
元素中remove()
了我调用remove()
的option
。 I can't find any documentation on this method of an option
element. 我找不到任何关于这个
option
元素方法的文档。
Is this an undocumented method? 这是一种无证的方法吗? If not, why does
remove()
work on the option
element? 如果没有,为什么
remove()
处理option
元素?
Here's a quick example. 这是一个简单的例子。 We'll create a
select
with 5 option
s, then remove specific options by calling options[N].remove()
where options
is an array containing the 5 options. 我们将创建一个带有5个
option
的select
,然后通过调用options[N].remove()
删除特定选项,其中options
是包含5个选项的数组。
const selectEl = document.querySelector('select') const optionEls = Array.from(selectEl.querySelectorAll('option')) optionEls[1].remove() optionEls[2].remove() optionEls[3].remove()
<select> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> <option>Option 4</option> <option>Option 5</option> <select>
.remove()
也是一种DOM
方法。
https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
The ChildNode.remove() method removes the object from the tree it belongs to.
ChildNode.remove()方法从它所属的树中删除对象。
Please note, however, there is currently no Internet Explorer support for this method. 但请注意,此方法目前没有Internet Explorer支持。
When there are no arguments passed to the remove function, Child node's remove function gets called and thus the node gets removed from DOM. 当没有参数传递给remove函数时,会调用Child节点的remove函数,从而从DOM中删除该节点。
It is clearly documented 它清楚地记录在案
The HTMLSelectElement.remove() method removes the element at the specified index from the options collection for this select element.
HTMLSelectElement.remove()方法从select元素的options集合中删除指定索引处的元素。
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/remove https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/remove
ChildNode.remove , the method that gets called when remove is called without arguments on a HTMLSelectElement. ChildNode.remove ,在HTMLSelectElement上调用remove而不带参数时调用的方法。
If you look close at the image, you can find the Option's Child node is a Text node and that gets removed. 如果仔细观察图像,可以发现Option的Child节点是Text节点,并且会被删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.