Last active
June 26, 2016 17:18
-
-
Save bogutski/c393a2b7a45c35a7a261b8ffbfc257de 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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<ul> | |
<li>Животные | |
<ul> | |
<li>Млекопитающие | |
<ul> | |
<li>Коровы</li> | |
<li>Ослы</li> | |
<li>Собаки</li> | |
<li>Тигры</li> | |
</ul> | |
</li> | |
<li>Другие | |
<ul> | |
<li>Змеи</li> | |
<li>Птицы</li> | |
<li>Ящерицы</li> | |
</ul> | |
</li> | |
</ul> | |
</li> | |
<li>Рыбы | |
<ul> | |
<li>Аквариумные | |
<ul> | |
<li>Гуппи</li> | |
<li>Скалярии</li> | |
</ul> | |
</li> | |
<li>Морские | |
<ul> | |
<li>Морская форель</li> | |
</ul> | |
</li> | |
</ul> | |
</li> | |
</ul> | |
<script> | |
// Виктор Богуцкий | |
// Get container for counting | |
let mainUl = document.body.children[0]; | |
// Count of elements in container | |
let liCount = document.getElementsByTagName('li').length; | |
for (let i = 0; i < liCount; i++) { | |
cnt(mainUl.getElementsByTagName('li')[i]); | |
} | |
function cnt(elem) { | |
let label = elem.firstChild.nodeValue.trim(); | |
let count = " - " + elem.getElementsByTagName('li').length; | |
// Change HTML | |
if (elem.innerHTML.indexOf('\n') > 0) { | |
elem.innerHTML = elem.innerHTML.replace('\n', count); | |
} else { | |
elem.innerHTML = elem.innerHTML + count; | |
} | |
// In console | |
console.log(label, count); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment