сохранить во фрагменте, потом вставить
<head>
<script>
window.onload = function () {
function createTree(el, par) {
var ul = document.createElement('ul');
par.appendChild(ul);
var childs = el.children;
var len = childs.length;
for (var i = 0; i < len; i++) {
var li = document.createElement('li');
li.innerHTML = childs[i].tagName;
ul.appendChild(li);
createTree(childs[i], ul);
}
}
var frag = document.createDocumentFragment();
createTree(document.body, frag);
document.body.appendChild(frag);
}
</script>
</head>
<body>
<div>
<div>
<span>span</span>
<button>button1</button>
<input>
</div>
<div>
<form>
<input>
<input>
<span>span2</span>
<button>button2</button>
</form>
</div>
</div>
<div>
<div>
<span>span</span>
<button>button1</button>
<input>
</div>
<div>
<form>
<input>
<input>
<span>span2</span>
<button>button2</button>
</form>
</div>
</div>
</body>