给你一棵树
需要3个文件
1.tree.js
2.tree.css
3.left.jsp
代码分比如下:
-----------------------------------------------------------1.tree.js
/**************************************************************************
Copyright (c) 2001 Geir Landr?(drop@destroydrop.com)
JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
Version 0.96
This script can be used freely as long as all copyright messages are
intact.
**************************************************************************/
// Arrays for nodes and icons
var nodes = new Array();;
var openNodes = new Array();
var icons = new Array(6);
// Loads all icons that are used in the tree
function preloadIcons() {
icons[0] = new Image();
icons[0].src = "../images/plus.gif";
icons[1] = new Image();
icons[1].src = "../images/plusbottom.gif";
icons[2] = new Image();
icons[2].src = "../images/minus.gif";
icons[3] = new Image();
icons[3].src = "../images/minusbottom.gif";
icons[4] = new Image();
icons[4].src = "../images/folder.gif";
icons[5] = new Image();
icons[5].src = "../images/folderopen.gif";
}
// Create the tree
function createTree(arrName, startNode, openNode, rootDesc) {
nodes = arrName;
if (nodes.length > 0) {
preloadIcons();
if (startNode == null) startNode = 0;
if (openNode != 0 || openNode != null) setOpenNodes(openNode);
//设置第一级的目录自动展开
//for (var j=0; j<nodes.length; j++) {
// var nodeValues = nodes[j].split("|");
// setOpenNodes(nodeValues[0]);
//}
var recursedNodes = new Array();
addNode(startNode, recursedNodes);
}
}
// Returns the position of a node in the array
function getArrayId(node) {
for (i=0; i<nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[0]==node) return i;
}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
for (i=0; i<nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[0]==openNode) {
openNodes.push(nodeValues[0]);
setOpenNodes(nodeValues[1]);
}
}
}
// Checks if a node is open
function isNodeOpen(node) {
for (i=0; i<openNodes.length; i++)
if (openNodes[i]==node) return true;
return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
for (i=0; i< nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[1] == parentNode) return true;
}
return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
var lastChild = 0;
for (i=0; i< nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[1] == parentNode)
lastChild = nodeValues[0];
}
if (lastChild==node) return true;
return false;
}
// Adds a new node in the tree
function addNode(parentNode, recursedNodes) {
for (var i = 0; i < nodes.length; i++) {
var nodeValues = nodes[i].split("|");
if (nodeValues[1] == parentNode) {
var ls = lastSibling(nodeValues[0], nodeValues[1]);
var hcn = hasChildNode(nodeValues[0]);
var ino = isNodeOpen(nodeValues[0]);
// Write out line & empty icons
for (g=0; g<recursedNodes.length; g++) {
if (recursedNodes[g] == 1) document.write("<img src=\"../images/line.gif\" align=\"absbottom\" alt=\"\" />");
else document.write("<img src=\"../images/empty.gif\" align=\"absbottom\" alt=\"\" />");
}
// put in array line & empty icons
if (ls) recursedNodes.push(0);
else recursedNodes.push(1);
// Write out node name
document.write(nodeValues[2]);
// End link
document.write("</a><br />");
// If node has children write out divs and go deeper
if (hcn) {
document.write("<div id=\"div" + nodeValues[0] + "\"");
if (!ino) document.write(" style=\"display: none;\"");
document.write(">");
addNode(nodeValues[0], recursedNodes);
document.write("</div>");
}
// remove last line or empty icon
recursedNodes.pop();
}
}
}
// Opens or closes a node
function oc(node, bottom) {
var theDiv = document.getElementById("div" + node);
var theJoin = document.getElementById("join" + node);
var theIcon = document.getElementById("icon" + node);