변수 children는 NodeList인스턴스이고 NodeLists는 참이 아니므로 메서드를 Array상속하지 않습니다 forEach.
또한 일부 브라우저는 실제로 지원합니다. nodeList.forEach
ES5
slicefrom Array을 사용 NodeList하여을 적절한 Array.
var array = Array.prototype.slice.call(children);
단순히을 사용 하여 as 컨텍스트 call를 호출 forEach하고 전달할 수도 NodeList있습니다.
[].forEach.call(children, function(child) {});
ES6
당신이 사용할 수있는 from당신을 변환하는 방법을 NodeList로 Array.
var array = Array.from(children);
또는 다음 과 같이 스프레드 구문을... 사용할 수도 있습니다.
let array = [ ...children ];
사용할 수있는 해킹입니다 NodeList.prototype.forEach = Array.prototype.forEach그리고 당신은 사용할 수있는 forEach모든으로 NodeList그들에게 때마다 변환 할 필요없이.
NodeList.prototype.forEach = Array.prototype.forEach
var children = element.childNodes;
children.forEach(function(item){
console.log(item);
});
좋은 설명과이를 수행하는 다른 방법은 NodeLists, Arrays, NodeLists 변환 및 DOM 이해에 대한 포괄적 인 내용을 참조하십시오 .