en.javascript.info/2-ui/1-document/07-modifying-document/8-tree-count/solution.view/index.html
Ilya Kantor d85be5f17b up
2017-03-05 19:49:29 +03:00

56 lines
1.1 KiB
HTML

<!DOCTYPE HTML>
<html>
<body>
<ul>
<li>Animals
<ul>
<li>Mammals
<ul>
<li>Cows</li>
<li>Donkeys</li>
<li>Dogs</li>
<li>Tigers</li>
</ul>
</li>
<li>Other
<ul>
<li>Snakes</li>
<li>Birds</li>
<li>Lizards</li>
</ul>
</li>
</ul>
</li>
<li>Fishes
<ul>
<li>Aquarium
<ul>
<li>Guppy</li>
<li>Angelfish</li>
</ul>
</li>
<li>Sea
<ul>
<li>Sea trout</li>
</ul>
</li>
</ul>
</li>
</ul>
<script>
let lis = document.getElementsByTagName('li');
for (let li of lis) {
// get the count of all <li> below this <li>
let descendantsCount = li.getElementsByTagName('li').length;
if (!descendantsCount) continue;
// add directly to the text node (append to the text)
li.firstChild.data += ' [' + descendantsCount + ']';
}
</script>
</body>
</html>