I've just started reading up on JavaScript and I'm trying to write a small recursive function that would search through given nodes and return a list of values as a string.
My HTML structure could be something like
<div id="parentfolder">parentfolder1
<div class ="item1">item1</div>
<div class ="item2">item2</div>
<div id="parentfolder">parentfolder2
<div class ="item1">item1</div>
<div class ="item2">item2</div>
</div>
</div>
And Here is my Javascript function:
function jsoncreator(parentfolderclass){
var jstring = '';
//get first occurance of parent folder
var parentfolder = document.getElementById(parentfolderclass);
var childnodes = parentfolder.childNodes;
for (property in childnodes){
jstring += property+ childnodes[property];
if(childnodes[property] === parentfolderclass){
jsoncreator(parentfolderclass);
jstring += childnodes[property].value + '<br>';
}
else{
//jstring += childnodes[i].value + '<br>';
}
}
document.write(jstring);
}
All im getting back is
0[object Text]1[object HTMLDivElement]2[object Text]3[object HTMLDivElement]4[object Text]5[object HTMLDivElement]6[object Text]length7itemfunction item() { [native code] }
When I try to print the childnodes values, I get a bunch of undefined returns.
If anybody could explain what I'm doing wrong, I'd really appreciate it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…