我正面临两个问题 1.我没有得到正确的HTML结构,见下图
我在寻找html结构:
以下是我尝试的代码:
var input = "[2030,2031,2032,2033,2034]";
var output = input.Replace("[", string.Empty).Replace("]", string.Empty).Split(',');
的jsfiddle
答案 0 :(得分:1)
这就是你要找的东西吗?
$(document).ready(function() {
var treeJson = {
"values": [{
"tree_title": "FashionWorld1",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel11",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel21"
},
{
"tree_title": "SubmenuLevel22"
},
{
"tree_title": "SubmenuLevel23"
}
]
}]
},
{
"tree_title": "SubmenuLevel12"
},
{
"tree_title": "SubmenuLevel13"
}
]
}]
},
{
"tree_title": "FashionWorld2",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel11",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel21"
},
{
"tree_title": "SubmenuLevel22"
},
{
"tree_title": "SubmenuLevel23"
}
]
}]
},
{
"tree_title": "SubmenuLevel12"
},
{
"tree_title": "SubmenuLevel13"
}
]
}]
},
{
"tree_title": "FashionWorld3",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel11",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel21"
},
{
"tree_title": "SubmenuLevel22"
},
{
"tree_title": "SubmenuLevel23"
}
]
}]
},
{
"tree_title": "SubmenuLevel12"
},
{
"tree_title": "SubmenuLevel13"
}
]
}]
},
{
"tree_title": "FashionWorld4",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel11",
"tree_image": "img_arrow",
"tree_image_position": "1",
"tree_image": "FashionWorld",
"isopen": "0",
"child": [{
"values": [{
"tree_title": "SubmenuLevel21"
},
{
"tree_title": "SubmenuLevel22"
},
{
"tree_title": "SubmenuLevel23"
}
]
}]
},
{
"tree_title": "SubmenuLevel12"
},
{
"tree_title": "SubmenuLevel13"
}
]
}]
}
]
};
var treeParentArr = treeJson.values;
var tree = buildNodes(treeJson, $("#tree"));
});
function buildNodes(node, parent) {
$(node.values).each(function() {
var element = this;
var listItem = $("<li />", {
text: this.tree_title
})
if (this.hasOwnProperty("child")) {
var tree = $("<ul />");
buildNodes(this.child[0], tree);
listItem.append(tree)
}
parent.append(listItem);
});
}
但有一个问题:为什么你需要values
里面的嵌套child
,因为它包含一个数组,你可以使用例如childs
来保存值对象?