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;
}


Voorbeeld heb ik gepost in PasteBin:
http://phphulp.pastebin.com/m7afec883
Ik heb net je functie getest, maar het werkt wel redelijk hoor, ik mis alleen wel de 1e li (en daarnaast is de functie ook niet recursief)



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="www.serials.ws" />

	<title>Untitled 2</title>
	<script src="mootools-1.2.1-core-nc.js"></script>
	<script>
	function FetchIDs(el){
    this.ret = new Array;
    
    if(el.getChildren('li').length > 0){
    	alert('test_successfull');
    	this.ret.push('var');
        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;
}

function test(){
	document.getElementById('test').innerHTML = FetchIDs(document.getElementById('testing'));
	//alert(FetchIDs(document.getElementById('menu')));
	}
	</script>
</head>

<body onload="test();">
<div id="menu">
	<ul id="testing">
		<li id="no1">List
			<ul>
				<li id="no1-1">List</li>
				<li id="no1-2">List</li>
				<li id="no1-3">List</li>
				<li id="no1-4">List</li>
				<li id="no1-5">List</li>
			</ul>
		</li>
		<li id="no2">List
			<ul>
				<li id="no2-1">List</li>
				<li id="no2-2">List</li>
				<li id="no2-3">List</li>
				<li id="no2-4">List</li>
				<li id="no2-5">List</li>
			</ul>
		</li>
		<li id="no3">List</li>
		<li id="no4">List</li>
		<li id="no5">List</li>
		<li id="no6">List</li>
	</ul>
</div>
<div id="test"></div>

</body>
</html>
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).

Bedankt voor de moeite Robert ;)

Reageren