En Javascript Comment créer un tableau de tous les éléments du DOM, commandé par leur profondeur, donc quelque chose comme ça …
<body> <1stChild> <1stGrandchild> <2ndChild> <3rdChild> <2ndGrandchild> <3rdGrandchild> <1stGreatgrandchild> <2stGreatgrandchild> <4thGrandchild> <4thChild> <etc etc>
Ressemblerait à ça …
["body", "1stChild", "2ndChild", "...", "lastChild", "1stGrandchild", "...", "lastGrandchild", "1stGreatgrandchild", "...", "lastGreatgrandchild" "etc. etc."]
J'ai une solution jQuery mais je voudrais un javascript pur
Cela modifie la walkTheDOM()
de CrockfordTheDOM walkTheDOM()
pour accomplir ce dont vous avez besoin.
var output = [], currentCount = 0, depth = 0; function walkTheDOM(node, func) { currentCount++; depth++; func(node); node = node.firstChild; while (node) { walkTheDOM(node, func); node = node.nextSibling; } depth--; } function getDepths(node) { if (node.nodeType !== 3) { output.push ({ node: node, depth: depth, encountered: currentCount }); } } walkTheDOM(document.body, getDepths); output.sort(function(a, b) { return a.depth === b.depth ? a.encountered - b.encountered : a.depth - b.depth; }); console.log(output);
<div class="first"> <div class="second"> <div class="third"></div> <div class="fourth"> <div class="fifth"></div> </div> </div> <div class="sixth"></div> <div class="seventh"> <div class="eighth"></div> </div> <div class="ninth"></div> </div> <div class="tenth"></div>
D'accord, je l'ai compris, c'était très simple à la fin.
// Every Element in the DOM. var allElements = document.getElementsByTagName('*'), // All the Element's children sorted by depth, // ie. body, then body's children, grandchildren, // so on and so forth. sortedByDepth = []; // for every element for(var i = 0; i<allElements.length; ++i) { // grab Its children var allChildren = allElements[i].children; // for every grabbed child for(var j = 0; j<allChildren.length; ++j){ // Add it to the sortedByDepth array sortedByDepth = sortedByDepth.concat(allChildren[j]); } } console.log(sortedByDepth);