我已经为这项任务苦苦挣扎了一段时间,并决定需要您的帮助。我怎样才能让所有的名字按行执行(html head tag body h1 div...)。
我使用递归方法编写了一段代码,但某处失败了。执行“无法读取未定义的属性‘forEach’”。你能告诉我错误在哪里并提示解决它吗?
"use strict";
var treeString;
var Node = require("./Node");
function TreeView(result){
treeString = "";
createTreeView(result, 0);
}
var createTreeView = function (current, level) {
treeString += current.children;
current.children.forEach(function(childObj) {
createTreeView(childObj, level + 1);
});
};
TreeView.prototype.toString = function (){
return treeString;
};
// Exports
module.exports = TreeView;
最佳答案
这应该迭代所有 dom 元素。粘贴到控制台,看看会发生什么。
var currentNode,
ni = document.createNodeIterator(document.documentElement, NodeFilter.SHOW_ELEMENT);
while(currentNode = ni.nextNode()) {
console.log(currentNode.nodeName);
关于javascript - 从对象及其子对象中获取所有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656305/