62,623
社区成员
发帖
与我相关
我的任务
分享<html>
<head>
<script type="text/javascript">
function Tree(name, children) {
this.name = name || "";
this.children = children || [];
this.appendChild = function(child) {
this.children.push(child);
}
this.toString = function() {
var result = "";
result += "<div>|- " + this.name + "</div>";
for (var i = 0; i < this.children.length; i++)
result += "<div style='margin-left: 1em'>" + this.children[i] + "</div>";
return result;
}
}
var tree = new Tree("中国");
tree.appendChild(new Tree("广东省", [new Tree("广州市"), new Tree("茂名市")]));
</script>
</head>
<body onload="document.getElementById('tree').innerHTML = tree">
<div id="tree"></div>
</body>
</html>