最初のオプション:forEachを間接的に呼び出す
parent.childrenオブジェクトのような配列です。次のソリューションを使用します。
const parent = this.el.parentElement;
Array.prototype.forEach.call(parent.children, child => {
console.log(child)
});
parent.childrenであるNodeListため、オブジェクトのような配列であるタイプ:
lengthノードの数を示すプロパティが含まれています
- 各ノードは、0から始まる数値名のプロパティ値です。
{0: NodeObject, 1: NodeObject, length: 2, ...}
この記事の詳細をご覧ください。
2番目のオプション:反復可能なプロトコルを使用する
parent.childrenですHTMLCollection:実装する反復可能なプロトコルを。ES2015環境では、HTMLCollection反復可能オブジェクトを受け入れる任意の構造でを使用できます。
HTMLCollectionスプレッドオペレーターで使用:
const parent = this.el.parentElement;
[...parent.children].forEach(child => {
console.log(child);
});
または、for..ofサイクル(これは私の優先オプションです):
const parent = this.el.parentElement;
for (const child of parent.children) {
console.log(child);
}