简体   繁体   English

为什么这不从<select>元素?

[英]Why doesn't this remove all of the items from the <select> element?

I have a function that is supposed to remove all child elements from a <select> on the onfocus event.我有一个函数应该从onfocus事件的<select>删除所有子元素。 (this is soon to be followed by filling the <select> with values from an AJAX call) For some reason, it only removes one of the elements the first time it is called , and then proceeds to do nothing all subsequent calls. (这之后很快就会用来自 AJAX 调用的值填充<select> )出于某种原因,它只在第一次被调用时删除一个元素,然后在所有后续调用中什么都不做。 Does anyone have any clue as to why this might be happening?有没有人知道为什么会发生这种情况?

JS Code: JS代码:

function handle(e) {
  for(var i = 0; i < e.children.length - 1; i++) {
    e.removeChild(e.children[i]);
  }
}

i'm guessing handle() is your onfocus event handler.我猜handle()是您的onfocus事件处理程序。 in which case e is your event object and doesn't have any children property在这种情况下, e是您的事件对象,并且没有任何children属性

why not just use select.innerHTML = ""为什么不直接使用select.innerHTML = ""

So the answer is simply to remove them in inverse order, as to avoid the effects of a indexing system quite similar to the VB.NET List Class.因此,答案只是以相反的顺序删除它们,以避免与 VB.NET 列表类非常相似的索引系统的影响。 (That is that it reorganizes itself every time a change is made. (也就是说,每次进行更改时它都会重新组织自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM