这个是我上次给人做的用js代码写的一一个树形菜单的代码 。。你看看。。很有帮助的~~祝你好运。
var openImg = new Image();
var closedImg = new Image();
function showBranch(branch) {
var objBranch = document.getElementById(branch).style;
if (objBranch.display == "block")
objBranch.display = "none";
else
objBranch.display = "block";
swapFolder('I' + branch);
}
function swapFolder(img) {
objImg = document.getElementById(img);
if (objImg.src.indexOf('closed.gif') > -1)
objImg.src = openImg.src;
else
objImg.src = closedImg.src;
}
function tree() {
this.branches = new Array();
this.add = addBranch;
this.write = writeTree;
this.GetBranchSize = GetBranchSize;
}
function GetBranchSize() {
return this.branches.length;
}
function addBranch(branch) {
this.branches[this.branches.length] = branch;
}
function writeTree() {
var treeString = '';
var numBranches = this.branches.length;
for (var i = 0; i < numBranches; i++)
treeString += this.branches[i].write();
document.write(treeString);
}
function branch(id, text) {
this.id = id;
this.text = text;
this.write = writeBranch;
this.add = addLeaf;
this.leaves = new Array();
}
function addLeaf(leaf) {
this.leaves[this.leaves.length] = leaf;
}
function writeBranch() {
var branchString = '<span class="branch"' + 'onClick="showBranch(\'' + this.id + '\')"';
branchString += '><img width=15 height=15 src="images/closed.gif" id="I' + this.id + '" /> ' + this.text;
branchString += '</span>';
branchString += '<span class="leaf" id="';
branchString += this.id + '">';
var numLeaves = this.leaves.length;
for (var j = 0; j < numLeaves; j++) branchString += this.leaves[j].write();
branchString += '</span>';
return branchString;
}
function leaf(text, link) {
this.text = text;
this.link = link;
this.write = writeLeaf;
}
function writeLeaf() {
var leafString = '<img src="images/doc.gif" border="0" /> <a onmouseover="ChangeBackColor(this,true)" onmouseout="ChangeBackColor(this,false)" href="' + this.link + '" target="OfficeMain">';
leafString += this.text;
leafString += '</a><br/>';
return leafString;
}
/*此函数用于当用户选择顶部的菜单导航时展开某个菜单*/
function ShowTreeNode(index) {
var branches_length = myTree.GetBranchSize();
for (var i = 1; i < branches_length; i++) {
var objBranch = document.getElementById("branch" + i).style;
objImg = document.getElementById("Ibranch" + i);
if (index == i) {
objBranch.display = "block";
objImg.src = openImg.src;
}
else {
objBranch.display = "none";
objImg.src = closedImg.src;
}
}
}
/*当鼠标移进或移出某个菜单上面时改变背景颜色*/
function ChangeBackColor(object, flag) {
if (flag) {
object.style.backgroundColor = "#FF0000";
object.style.color = "#30F";
}
else {
object.style.backgroundColor = "";
object.style.color = "#F0F";
}
}
var myTree = new tree();
/*向html页面写入菜单导航树*/
function WriteTreeInfo() {
openImg.src = "images/open.gif";
closedImg.src = "images/closed.gif";
var branches = new Array();
branches[1] = new branch('branch1', '房屋信息管理');
var branch1_leaf1 = new leaf('房屋信息登记', 'hourse/hourse_add.aspx');
var branch1_leaf2 = new leaf('房屋信息维护', 'hourse/hourse_manage.aspx');
var branch1_leaf3 = new leaf('查看我发布的信息', 'hourse/displayMyPublic.aspx');
var branch1_leaf4 = new leaf('热门房屋信息', 'hourse/HotSearch.aspx');
var branch1_leaf5 = new leaf('查看我的订单信息', 'hourse/displayMyOrder.aspx');
branches[1].add(branch1_leaf1);
branches[1].add(branch1_leaf2);
branches[1].add(branch1_leaf3);
branches[1].add(branch1_leaf4);
branches[1].add(branch1_leaf5);
branches[2] = new branch('branch2', '入住管理');
var branch2_leaf1 = new leaf('户主入住登记', 'hourse/UsersInfoAdd.aspx');
var branch2_leaf2 = new leaf('户主入住情况报表', 'hourse/UsersInfoDisplay.aspx');
var branch2_leaf3 = new leaf('用户管理', 'hourse/UserMgr.aspx');
branches[2].add(branch2_leaf1);
branches[2].add(branch2_leaf2);
branches[2].add(branch2_leaf3);
branches[3] = new branch('branch3', '电费管理');
var branch3_leaf1 = new leaf('收取电费登记', 'hourse/ElectriCharge.aspx');
var branch3_leaf2 = new leaf('电费记录信息管理', 'hourse/ElectriCharge_display.aspx');
var branch3_leaf3 = new leaf('电费设置管理', 'hourse/SettingElectirPrice.aspx');
branches[3].add(branch3_leaf1)
branches[3].add(branch3_leaf2);
branches[3].add(branch3_leaf3);
branches[4] = new branch('branch4', '水费管理');
var branch4_leaf1 = new leaf('收取住户水费', 'hourse/WaterFeeMgraspx.aspx');
var branch4_leaf2 = new leaf('水费记录信息管理', 'hourse/DisplayWaterFee.aspx');
var branch4_leaf3 = new leaf('水费记录信息管理', 'hourse/SettingWaterPrice.aspx');
branches[4].add(branch4_leaf1)
branches[4].add(branch4_leaf2);
branches[4].add(branch4_leaf3);
branches[5] = new branch('branch5', '费用统计');
var branch5_leaf1 = new leaf('查看费用', 'hourse/CostMgr.aspx');
branches[5].add(branch5_leaf1)
branches[6] = new branch('branch6', '留言板');
var branch6_leaf1 = new leaf('开始留言', 'hourse/MessageBoard.aspx');
var branch6_leaf2 = new leaf('查看留言记录', 'hourse/displayMessage.aspx');
branches[6].add(branch6_leaf1);
branches[6].add(branch6_leaf2);
branches[7] = new branch('branch7', '系统管理');
var branch7_leaf1 = new leaf('修改密码', 'hourse/modifyPassword.aspx');
var branch7_leaf3 = new leaf('用户管理', 'hourse/UserMgr.aspx');
var branch7_leaf4 = new leaf('关于系统', 'about.html');
var branch7_leaf5 = new leaf('系统说明', 'desk.aspx');
var branch7_leaf6 = new leaf('公告栏', 'PublicNotice.htm');
branches[7].add(branch7_leaf1)
branches[7].add(branch7_leaf3);
branches[7].add(branch7_leaf4);
branches[7].add(branch7_leaf5);
branches[7].add(branch7_leaf6);
myTree.add(branches[1]);
myTree.add(branches[2]);
myTree.add(branches[3]);
myTree.add(branches[4]);
myTree.add(branches[5]);
myTree.add(branches[6]);
myTree.add(branches[7]);
myTree.add(new leaf('退出系统', 'logout.aspx'));
myTree.write();
}