<script>
var rootNode = document.getElementById("maindiv"); //指定区域的根元素
var allNodes = new Array();
function GetNode(obj) {
!obj.visited ? !function () {
allNodes.push(obj);
obj.visited = true;
}() : "";
if (obj.hasChildNodes() && obj.innerHTML != obj.innerText &&
!obj.firstElementChild.visited) {
GetNode(obj.firstElementChild);
} else {
if (obj == rootNode) return;
obj.nextElementSibling != null ?
GetNode(obj.nextElementSibling) : GetNode(obj.parentElement);
}
}
GetNode(rootNode);
for (var i = 1; i < allNodes.length; i++) {
!allNodes[i].id == "" && allNodes[i].tagName == "BUTTON" ?
(function () {
allNodes[i].style.backgroundColor = "red";
for (var j = i; j < allNodes.length;j++)
{
if (allNodes[j].tagName == "DIV") {
allNodes[j].innerHTML == "" ?
allNodes[j].innerHTML = '<a href="http://www.baidu.com">baidu</a>' :
allNodes[j].style.backgroundColor = "blue";
break;
}
}
}()) : "";
}
</script>
<script>
//创建节点对象
var Node = (function () {
function Node(theId, theText, thePid ,theLevel) {
this.id = theId, this.text = theText,
this.pid = thePid, this.level = theLevel;
}
return Node;
})();
var Nodes = new Array();
Nodes.push(new Node(0, "root", 0, 0));
Nodes.push(new Node(1, "a", 0, 2));
Nodes.push(new Node(2, "a", 1, 3));
Nodes.push(new Node(4, "a", 1, 3));
Nodes.push(new Node(5, "a", 0, 2));
var abcArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
function CreateNode()
{
var tmpNodes = new Array();
for (var i = 0; i < Nodes.length; i++) {
if (Nodes[i].level != 3)
tmpNodes.push(Nodes[i]);
}
var pidIndex = parseInt(Math.random() * tmpNodes.length);
var textIndex = parseInt(Math.random() * abcArr.length);
var id=parseInt(Math.random() * 1000);
return new Node(id, abcArr[textIndex], tmpNodes[pidIndex].id, tmpNodes[pidIndex].level + 1);
}
for(var i=1;i<=46;i++)
{
Nodes.push(CreateNode());
}
for (var i = 1; i <=50; i++)
{
document.write(Nodes[i].id + "--" + Nodes[i].text + "--" + Nodes[i].pid + "</br>");
}
</script>