樹菜單js網上多得是,自己寫了一個樹菜單類,自認為還不錯,公開大家共用和探討,和大家一起進步最快樂!!
/* KuoMart.com__MyKuoMart , Create By ybcola(at)163.com , 2006-7-2
Last Modified 2006-7-2 , ybcola@163.com */
<!--
function Dtree(){
this.MenuGroupClose="images/treeview_close.gif"; //菜單關閉表徵圖
this.MenuGroupOpen="images/treeview_open.gif"; //菜單開啟表徵圖
this.MenuItemClass=""; //功能表項目樣式
this.MenuItemInfoClass=""; //菜單連結樣式
this.MenuItemlPad="12px";//下級菜單縮排仁值
this.DefaultGroupState=false; //菜單組裝入時預設狀態
document.write("<div id=\"treeroot\"></div>");
this.Node=document.getElementById("treeroot");
//0結點DOM對像,數組1為串連顯示,2為串連href,3為串連target,4為串連顯示前表徵圖
this.AddItem=function(menuinfo){
var div=creatediv();
var div1=creatediv();
var ico=[this.MenuGroupClose,this.MenuGroupOpen];
div.className=this.MenuItemClass;
div1.style.marginLeft=this.MenuItemlPad;
div1.style.display=this.DefaultGroupState?"":"none";
var Node=menuinfo[0]?menuinfo[0][1]:this.Node;
var iteminfo="";
if(menuinfo[4]) iteminfo="<img src=\""+menuinfo[4]+"\" align=\"absmiddle\">";
if(menuinfo[2]) iteminfo+=" <a href=\""+menuinfo[2]+"\" target=\""+menuinfo[3]+"\" class=\""+this.MenuItemInfoClass+"\">";
iteminfo+=menuinfo[1];
if(menuinfo[2])iteminfo+="</a>";
div.innerHTML=iteminfo;
Node.appendChild(div);
Node.appendChild(div1);
if(menuinfo[0]){
if(!menuinfo[0][0].havechild) {
var as=document.createElement("img");
as.src=ico[1];
as.align="absmiddle";
as.onclick=function(){
swapshow(Node,as,ico);
}
menuinfo[0][0].insertBefore(as,menuinfo[0][0].firstChild);
as.insertAdjacentText("afterEnd"," ");
menuinfo[0][0].havechild=true;
}
}
return [div,div1];
}
function creatediv(){
var div=document.createElement("div");
return div;
}
function swapshow(obj,ico,oo){
if(obj.style.display!=""){
obj.style.display="";
ico.src=oo[0];
}
else{
obj.style.display="none";
ico.src=oo[1];
}
}
}
-->