![](/img/trans.png)
[英]Why doesn't the splice method remove all the items from an array with matching property values?
[英]Why doesn't this remove all of the items from the <select> element?
我有一個函數應該從onfocus
事件的<select>
刪除所有子元素。 (這之后很快就會用來自 AJAX 調用的值填充<select>
)出於某種原因,它只在第一次被調用時刪除一個元素,然后在所有后續調用中什么都不做。 有沒有人知道為什么會發生這種情況?
JS代碼:
function handle(e) {
for(var i = 0; i < e.children.length - 1; i++) {
e.removeChild(e.children[i]);
}
}
我猜handle()
是您的onfocus
事件處理程序。 在這種情況下, e
是您的事件對象,並且沒有任何children
屬性
為什么不直接使用select.innerHTML = ""
因此,答案只是以相反的順序刪除它們,以避免與 VB.NET 列表類非常相似的索引系統的影響。 (也就是說,每次進行更改時它都會重新組織自己。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.