對象:製作樹對象
var src = {
0: {child: [1], title: "Books"},
1: {child: [2,3], title: "Programming"},
2: {title: "Languages"},
3: {child: [4,5],title: "Databases"},
4: {title: "MongoDB"},
5: {title: "dbm"}
};
你怎麼能在這個對象
<div id="tree">
<ul>
<li id="0">
<span>Books</span>
<ul>
<li id="1">
<span>Programming</span>
<ul>
<li id="2">
<span>Languages</span>
</li>
<li id="3">
<span>Databases</span>
<ul>
<li id="4">
<span>MongoDB</span>
</li>
<li id="5">
<span>dbm</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
爲了簡化樹的結構發生變化的形式建立一個樹,但本質 仍然存在。請告訴我。
我試過,但具有不同的數據結構
var data = {
0: {title: "Books"},
1: {pid: 0, title: "Programming"},
2: {pid: 1, title: "Languages", ch: 0},
3: {pid: 1, title: "Databases"},
4: {pid: 3, title: "MongoDB", ch: 0},
5: {pid: 3, title: "dmb", ch: 0}
};
function makeTree(data) {
var child = [];
var i = 0;
for (var key in data) {
if (data[key].hasOwnProperty('pid')) {
if (data[key].hasOwnProperty('ch')) {
child[i] = document.createElement('li');
child[i].setAttribute('id', key);
child[i].innerHTML = data[key].title + ' <span></span></li>';
}
else {
child[i] = document.createElement('li');
child[i].setAttribute('id', key);
child[i].innerHTML = data[key].title + '<span></span>';
child[i].innerHTML += '<ol><li id=' + key + '>' + data[key].title + '<span></span></li></ol>';
}
document.getElementById(data[key].pid + "").appendChild(child[i]);
i++;
}
else {
var li = document.createElement('li');
li.setAttribute('id', key);
li.innerHTML = data[key].title + '<span></span>'
tree.appendChild(li);
}
}
}
它的工作原理,但不正確
顯示你有什麼到目前爲止已經試過。 – Codor
請參考此鏈接http://www.latestcode.net/2010/03/how-to-create-tree-viewmenu.html –
我試過了,但具有不同的數據結構: – timkit