Misschien niet zon goed titel, maar ik zit met een probleem. Ik heb een scriptje dat door een UL/Li structuur moet cyclen, daar de id's van ophalen en daar dan vervolgens een array voor moet maken.
Alleen het script dat ik daarvoor heb gemaakt werkt niet, en ik hoop dat een van jullie misschien de fout kan vinden...
Hier is de JS:
function FetchIDs(el){
this.ret = new Array;
if(el.getChildren('li').length > 0){
el.getChildren('li').each(function(item){
if(item.getChildren('ul').length > 0){
var index = new Array(item.get('id'));
var na = index.concat(FetchIDs(item));
this.ret.push(na);
}else{
this.ret.push(item.get('id'));
}
},this);
}
return this.ret;
}
Ik heb hem opnieuw geschreven, mischien heeft iemand er nog wat aan. Hij werkt nu wel:
function getNodeIds(el){
var nodes = new Array;
if(el.hasChildNodes()){// So, first we check if the object is not empty, if the object has child nodes
var children = el.childNodes;
for(var i = 0; i < children.length; i++){
if(children[i].tagName != undefined && children[i].tagName.toLowerCase() == 'li'){// A li node
if(el.hasChildNodes()){
var index = children[i].id;
// Get First (Non-#Text) Child
var CC = children[i].firstChild;// Child to cycle over (CycleChild)
while(CC){
if(CC.tagName != undefined && CC.tagName.toLowerCase() == 'ul'){
break;
}
CC = CC.nextSibling;
}
// Cycle through that child
var nea = getNodeIds(CC);
// Clean up the output(For when all childs have empty UL's)
if(nea.length == 0){// If the thing contains an empty UL
var na = index;
}else{// The array contains elements
var na = [index].concat(nea);
}
// And add it to our array
nodes.push(na);
}else{
nodes.push(children[i].id);
}
}// It is an TextNode, and we don't want those
}
}
return nodes;
}
Wat het doet:
Hij cycled door een UL/LI structuur en maakt er een array structuur van (Van de id's).