여기에 select 요소와 몇 가지 옵션 요소가 있습니다. foreach 루프를 실행하여 모든 옵션 요소를 삭제하려고합니다. 그러나 처음 두 요소 만 제거되고 있습니다.이 오류는 무엇입니까? 암호?select 태그에서 모든 옵션을 제거합니다
<!DOCTYPE html>
<html>
<body>
<p id='item'></p>
<form>
remove all from fruit list:
<br>
<select id="mySelect" size="4" class='myclass' onChange='myFunction(this.className);'>
<option id='for_apple'>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>
<script>
let select_item = document.getElementById('mySelect');
let options=select_item.getElementsByTagName('option');
console.log('length is : '+options.length);
Array.prototype.forEach.call(options,(elem,index,arr) => {
console.log(options.length);
select_item.removeChild(elem);
});
</script>
</body>
</html>
내 생각 엔 options' 업데이트가 살고있는'이다 :이 개 요소를 제거한 후, 그 길이가 4-2 ==이되고, 그래서 루프가 멈 춥니 다. – melpomene
'select_item.innerHTML = "";'사용하기 : while (options.length) select_item.removeChild (options [0]);' –