Created
November 29, 2022 19:32
-
-
Save marsgpl/3a8cbb90f4e0c5d79aa8b65088e824c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<root> | |
root | |
<a> | |
a | |
<aa>aa</aa> | |
<bb>bb</bb> | |
</a> | |
<b>b | |
<ba>ba</ba> | |
<bb>bb</bb> | |
</b> | |
</root> | |
<script> | |
function DFS(rootNode) { | |
const next = [rootNode] | |
while (next.length) { | |
const node = next.pop() | |
if (node.nodeName === '#text') { continue } | |
console.log(node) | |
const { childNodes } = node | |
for (let i = childNodes.length - 1; i >= 0; --i) { | |
next.push(childNodes[i]) | |
} | |
} | |
} | |
DFS(document.querySelector('root')) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment