Вывод на экран и консоль DOM-дерева страницы HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<html>
<head>
<title>Задача</title>
<meta charset="utf-8">
<script type="text/javascript">
function run(object, string) {
for (var i = 0; i < object.childNodes.length; i++) {
var child = object.childNodes[i];
var result = document.getElementById("result");
if ((child.tagName != undefined) && (child.className != "stop")) {
var newDiv = document.createElement("div");
newDiv.innerHTML = string + " " + child.tagName;
result.appendChild(newDiv);
console.log(string + " " + child.tagName);
run(child, string + " " + child.tagName);
}
}
return;
}
</script>
</head>
<body>
<div>
<input type="text">
<input type="text">
<input type="textarea">
<div>
<div>
<div id="result" class="stop">
</div>
</div>
</div>
<div>
<input type="button" onclick="run(document.body, 'BODY');" value="Click!">
</div>
</div>
</body>
</html>