我正在尝试从头开始编写 getElementByClassName,但我不确定何时返回递归。这就是我想出的:
const getElementsByClassName = (nameOfClass, parent) => {
const result = []
for(let el of parent) {
// console.log(el.children)
if(el.firstElementChild != null) {
// do it again on node deeper
getElementsByClassName(nameOfClass, el.children)
}
if(el.className === nameOfClass) {
result.push(el)
}
}
// when do I want to return result?
console.log(result)
return result
};
问题是我每个子节点都有一个数组,而不是同一个数组结果中的所有内容。我该如何解决这个问题?
森林海
海绵宝宝撒
肥皂起泡泡
眼眸繁星
随时随地看视频慕课网APP
相关分类