Created
November 17, 2020 08:46
Revisions
-
karataev created this gist
Nov 17, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ function padding(depth) { return new Array(depth).fill(' ').join(''); } function walk(node, depth) { if (node.nodeType === 1) { console.log(padding(depth) + node.tagName); let children = Array.from(node.childNodes); children.forEach(item => walk(item, depth + 1)); } else if (node.nodeType === 3) { let text = node.textContent.trim(); if (text) console.log(padding(depth) + text); } } function walkTree(selector) { let root = document.querySelector(selector); walk(root, 0); } walkTree('#root');