![](/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.