Just a quick simple example:
function tree(data) {
if (typeof(data) == 'object') {
document.write('<ul>');
for (var i in data) {
document.write('<li>' + i);
tree(data[i]);
}
document.write('</ul>');
} else {
document.write(' => ' + data);
}
}
jQuery version:
function tree(data) {
if (typeof(data) == 'object') {
var ul = $('<ul>');
for (var i in data) {
ul.append($('<li>').text(i).append(tree(data[i])));
}
return ul;
} else {
var textNode = document.createTextNode(' => ' + data);
return textNode;
}
}
$(document.body).append(tree(data));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…