У меня есть <div>
с некоторым дочерним <div>
. Например.
<div id="niceParent">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Я попытался пропустить их с помощью функции forEach
, потому что я думал, что document.getElementById("niceParent").children
является массивом, так как я могу получить доступ к элементам с помощью
console.log(document.getElementById("niceParent").children[1]);
console.log(document.getElementById("niceParent").children[2]);
console.log(document.getElementById("niceParent").children[3]);
console.log(document.getElementById("niceParent").children[4]);
Следовательно, я попробовал
document.getElementById("niceParent").children.forEach(function(entry) {
console.log(entry);
});
который не работает. Я получаю
TypeError: document.getElementById(...).children.forEach is not a function
В качестве обходного пути я также попробовал его с гораздо более сложным циклом for..in
:
for (var i in document.getElementById("niceParent").children) {
if (document.getElementById("niceParent").children[i].nodeType == 1) console.log(document.getElementById("niceParent").children[i]);
}
который работал как ожидалось.
Почему?