1.新建HTML文件,导入三个文件
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="jquery-ztree-2.5.js"></script>
<link rel="stylesheet" href="zTreeStyle/zTreeStyle.css" type="text/css">
2.在html元素中选择一个树的容器
<ul id="tree" class="tree" style="width:230px; overflow:auto;"></ul>
3.在js中调用生成tree的方法zTree
附上测试源码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
body {
background-color: white;
margin:0; padding:0;
text-align: center;
}
div, p, table, th, td {
list-style:none;
margin:0; padding:0;
color:#333; font-size:12px;
font-family:dotum, Verdana, Arial, Helvetica, AppleGothic, sans-serif;
}
var zTree;
var demoIframe;
var setting = {
isSimpleData: true,
treeNodeKey: "id",
treeNodeParentKey: "pid",
showLine: true,
root:{
isRoot:true,
nodes:[]
}
};
zNodes =[
{"id":0,"pid":-1,"name":"亚洲"},
{"id":1,"pid":0,"name":"中国"},
{"id":10,"pid":1,"name":"北京","icon":"user.gif"},
{"id":11,"pid":1,"name":"上海","icon":"user.gif"},
{"id":12,"pid":1,"name":"天津","icon":"user.gif"},
{"id":13,"pid":1,"name":"重庆","icon":"user.gif"},
{"id":2,"pid":0,"name":"日本"},
{"id":20,"pid":2,"name":"东京"},
{"id":3,"pid":0,"name":"韩国"},
{"id":30,"pid":3,"name":"首尔"},
];
$(document).ready(function(){
zTree = $("#tree").zTree(setting, zNodes);
});
function loadReady() {
var h = demoIframe.contents().find("body").height();
if (h < 600) h = 600;
demoIframe.height(h);
}
<BODY>
<TABLE border=0 height=600px align=left>
<TR>
<TD width=230px align=left valign=top style="BORDER-RIGHT: #999999 1px dashed">
<ul id="tree" class="tree" style="width:230px; overflow:auto;"></ul>
</TD>
</TR>
</TABLE>