更新时间:2018/1/10 下午3:09:40
更新说明:优化默认打开当前选中的菜单,在节点上添加class“ontree”,添加了以下代码
$(".ontree").each(function(){
$(this).parents("ul").show();
$(this).parents("ul").siblings().addClass("open_menu").removeClass("close_menu");
});第一步:按照demo的结构搭建html,具体应用时,自定义树状菜单在页面中的位置可调整
<div class="centent"></div>
的css;
第二步:引入tree.css和treescroll.min.js;
第三步:初始化菜单时按需要在相应的位置设置打开或者关闭,打开使用“open_menu”,关闭使用“close_menu”;
接口返回的数据格式可参照:1 {…}
name 城中区
count 6
id 1
sort 0
childrenList […]
0 {…}
name 城中街道
count 2
id 2
sort 0
childrenList []
1 {…}
2 {…}
3 {…}
4 {…}
5 {…}
以下是动态构造树状的方法:
function buildMenu(rdata) {
var temp = '';
$(rdata).each(function() {
if (this.childrenList.length != 0) {
temp += '<li><div class="close_menu"><span></span><a title="' + this.name + '" id="' + this.id + '">' + this.name + '</a></div>';
temp += '<ul parentId="' + this.id + '" parentName="' + this.name + '" style="display:none;">';
temp += buildMenu(this.childrenList);
temp += '</ul></li>';
} else {
temp += '<li><a title="' + this.name + '" id="' + this.id + '">' + this.name + '</a></li>'
}
});
return temp;
}