hasChildNodes 总是 return 未定义
hasChildNodes always return undefined
我在研究递归函数:
function Recursive(JSObject) {
for(let JSChild of JSObject.children) {
console.log(JSChild.name);
console.log(JSChild.hasChildNodes);
/*if(JSChild.hasChildNodes) {
Recursive(JSChild);
}*/
}
}
不幸的是:
console.log(JSChild.hasChildNodes);
总是return "undefined" 而有些节点有一个或多个子节点。
此外,w3school 在 hasChildNodes 的末尾使用了一些括号,但是当我这样做时我的浏览器 (Chrome) return 这个错误:
JSChild.hasChildNodes is not a function
我不明白为什么没有任何效果。
如果你能帮助我,我将不胜感激。
编辑:(JSON 格式的对象)
{
"dir":"",
"name":"myProject",
"type":"folder",
"children":[
{
"dir":"myProject",
"name":"css",
"type":"folder",
"children":[
{
"dir":"myProject/css",
"name":"main.css",
"type":"file"
},
{
"dir":"myProject/css",
"name":"menu.css",
"type":"file"
},
{
"dir":"myProject/css",
"name":"user.css",
"type":"file"
}
]
},
{
"dir":"myProject",
"name":"fonts",
"type":"folder"
},
{
"dir":"myProject",
"name":"images",
"type":"folder",
"children":[
{
"dir":"myProject/images",
"name":"logo.png",
"type":"file",
"corrupted":true
}
]
},
{
"dir":"myProject",
"name":"index.html",
"type":"file"
},
{
"dir":"myProject",
"name":"js",
"type":"folder",
"children":[
{
"dir":"myProject/js",
"name":"controllers",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers",
"name":"core",
"type":"folder",
"children":[
{
"dir":"myProject/tempjs/controllerslates/core",
"name":"menu.js",
"type":"file"
}
]
},
{
"dir":"myProject/js/controllers",
"name":"errors",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers/errors",
"name":"error.js",
"type":"file"
}
]
},
{
"dir":"myProject/js/controllers",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers/home",
"name":"homePage.js",
"type":"file"
}
]
},
{
"dir":"myProject/js/controllers",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers/user",
"name":"list.js",
"type":"file"
},
{
"dir":"myProject/js/controllers/user",
"name":"login.js",
"type":"file"
},
{
"dir":"myProject/js/controllers/user",
"name":"profile.js",
"type":"file"
},
{
"dir":"myProject/js/controllers/user",
"name":"subscribe.js",
"type":"file"
}
]
}
]
},
{
"dir":"myProject/js",
"name":"libs",
"type":"folder",
"children":[
{
"dir":"myProject/js/libs",
"name":"handlebars.min.js",
"type":"file"
},
{
"dir":"myProject/js/libs",
"name":"jquery.min.js",
"type":"file"
},
{
"dir":"myProject/js/libs",
"name":"require.min.js",
"type":"file",
"corrupted":true
}
]
},
{
"dir":"myProject/js",
"name":"main.js",
"type":"file"
},
{
"dir":"myProject/js",
"name":"models",
"type":"folder",
"children":[
{
"dir":"myProject/js/models",
"name":"menu.js",
"type":"file"
},
{
"dir":"myProject/js/models",
"name":"user.js",
"type":"file"
},
{
"dir":"myProject/js/models",
"name":"users.js",
"type":"file"
}
]
}
]
},
{
"dir":"myProject",
"name":"ressources",
"type":"folder"
},
{
"dir":"myProject",
"name":"views",
"type":"folder",
"children":[
{
"dir":"myProject/templates",
"name":"core",
"type":"folder",
"children":[
{
"dir":"myProject/templates/core",
"name":"footer.html",
"type":"file"
},
{
"dir":"myProject/templates/core",
"name":"header.html",
"type":"file"
},
{
"dir":"myProject/templates/core",
"name":"menu.html",
"type":"file"
}
]
},
{
"dir":"myProject/templates",
"name":"errors",
"type":"folder",
"children":[
{
"dir":"myProject/templates/errors",
"name":"error401.html",
"type":"file"
},
{
"dir":"myProject/templates/errors",
"name":"error403.html",
"type":"file"
},
{
"dir":"myProject/templates/errors",
"name":"error404.html",
"type":"file",
"corrupted":true
}
]
},
{
"dir":"myProject/templates",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/templates/home",
"name":"homePage.html",
"type":"file"
}
]
},
{
"dir":"myProject/templates",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/templates/user",
"name":"list.html",
"type":"file"
},
{
"dir":"myProject/templates/user",
"name":"login.html",
"type":"file"
},
{
"dir":"myProject/templates/user",
"name":"profile.html",
"type":"file"
},
{
"dir":"myProject/templates/user",
"name":"subscribe.html",
"type":"file"
}
]
}
]
}
]
}
Object
没有属性或方法名为hasChildNodes
,是Node的方法。
最后,我会用
if(typeof(enfant.children) == "object")
检查父节点是否有子节点。
感谢回复我post的人。
我在研究递归函数:
function Recursive(JSObject) {
for(let JSChild of JSObject.children) {
console.log(JSChild.name);
console.log(JSChild.hasChildNodes);
/*if(JSChild.hasChildNodes) {
Recursive(JSChild);
}*/
}
}
不幸的是:
console.log(JSChild.hasChildNodes);
总是return "undefined" 而有些节点有一个或多个子节点。
此外,w3school 在 hasChildNodes 的末尾使用了一些括号,但是当我这样做时我的浏览器 (Chrome) return 这个错误:
JSChild.hasChildNodes is not a function
我不明白为什么没有任何效果。
如果你能帮助我,我将不胜感激。
编辑:(JSON 格式的对象)
{
"dir":"",
"name":"myProject",
"type":"folder",
"children":[
{
"dir":"myProject",
"name":"css",
"type":"folder",
"children":[
{
"dir":"myProject/css",
"name":"main.css",
"type":"file"
},
{
"dir":"myProject/css",
"name":"menu.css",
"type":"file"
},
{
"dir":"myProject/css",
"name":"user.css",
"type":"file"
}
]
},
{
"dir":"myProject",
"name":"fonts",
"type":"folder"
},
{
"dir":"myProject",
"name":"images",
"type":"folder",
"children":[
{
"dir":"myProject/images",
"name":"logo.png",
"type":"file",
"corrupted":true
}
]
},
{
"dir":"myProject",
"name":"index.html",
"type":"file"
},
{
"dir":"myProject",
"name":"js",
"type":"folder",
"children":[
{
"dir":"myProject/js",
"name":"controllers",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers",
"name":"core",
"type":"folder",
"children":[
{
"dir":"myProject/tempjs/controllerslates/core",
"name":"menu.js",
"type":"file"
}
]
},
{
"dir":"myProject/js/controllers",
"name":"errors",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers/errors",
"name":"error.js",
"type":"file"
}
]
},
{
"dir":"myProject/js/controllers",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers/home",
"name":"homePage.js",
"type":"file"
}
]
},
{
"dir":"myProject/js/controllers",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/js/controllers/user",
"name":"list.js",
"type":"file"
},
{
"dir":"myProject/js/controllers/user",
"name":"login.js",
"type":"file"
},
{
"dir":"myProject/js/controllers/user",
"name":"profile.js",
"type":"file"
},
{
"dir":"myProject/js/controllers/user",
"name":"subscribe.js",
"type":"file"
}
]
}
]
},
{
"dir":"myProject/js",
"name":"libs",
"type":"folder",
"children":[
{
"dir":"myProject/js/libs",
"name":"handlebars.min.js",
"type":"file"
},
{
"dir":"myProject/js/libs",
"name":"jquery.min.js",
"type":"file"
},
{
"dir":"myProject/js/libs",
"name":"require.min.js",
"type":"file",
"corrupted":true
}
]
},
{
"dir":"myProject/js",
"name":"main.js",
"type":"file"
},
{
"dir":"myProject/js",
"name":"models",
"type":"folder",
"children":[
{
"dir":"myProject/js/models",
"name":"menu.js",
"type":"file"
},
{
"dir":"myProject/js/models",
"name":"user.js",
"type":"file"
},
{
"dir":"myProject/js/models",
"name":"users.js",
"type":"file"
}
]
}
]
},
{
"dir":"myProject",
"name":"ressources",
"type":"folder"
},
{
"dir":"myProject",
"name":"views",
"type":"folder",
"children":[
{
"dir":"myProject/templates",
"name":"core",
"type":"folder",
"children":[
{
"dir":"myProject/templates/core",
"name":"footer.html",
"type":"file"
},
{
"dir":"myProject/templates/core",
"name":"header.html",
"type":"file"
},
{
"dir":"myProject/templates/core",
"name":"menu.html",
"type":"file"
}
]
},
{
"dir":"myProject/templates",
"name":"errors",
"type":"folder",
"children":[
{
"dir":"myProject/templates/errors",
"name":"error401.html",
"type":"file"
},
{
"dir":"myProject/templates/errors",
"name":"error403.html",
"type":"file"
},
{
"dir":"myProject/templates/errors",
"name":"error404.html",
"type":"file",
"corrupted":true
}
]
},
{
"dir":"myProject/templates",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/templates/home",
"name":"homePage.html",
"type":"file"
}
]
},
{
"dir":"myProject/templates",
"name":"home",
"type":"folder",
"children":[
{
"dir":"myProject/templates/user",
"name":"list.html",
"type":"file"
},
{
"dir":"myProject/templates/user",
"name":"login.html",
"type":"file"
},
{
"dir":"myProject/templates/user",
"name":"profile.html",
"type":"file"
},
{
"dir":"myProject/templates/user",
"name":"subscribe.html",
"type":"file"
}
]
}
]
}
]
}
Object
没有属性或方法名为hasChildNodes
,是Node的方法。
最后,我会用
if(typeof(enfant.children) == "object")
检查父节点是否有子节点。
感谢回复我post的人。