Skip to content

Instantly share code, notes, and snippets.

@marsgpl
Created November 29, 2022 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marsgpl/3a8cbb90f4e0c5d79aa8b65088e824c2 to your computer and use it in GitHub Desktop.
Save marsgpl/3a8cbb90f4e0c5d79aa8b65088e824c2 to your computer and use it in GitHub Desktop.
<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