树状导航栏的制作
<html> <head> </head> <body> <ul id="side_menu"> <li><a href="#">[-] 一级菜单</a> <ul> <li><a href="#">[-] 二级菜单</a> <ul> <li><a href="#">[-] 三级菜单</a></li> <li><a href="#">[-] 三级菜单</a></li> </ul> </li> <li><a href="#">[-] 二级菜单</a></li> </ul> </li> </ul> <body> <script type="text/javascript"> /*=======================树状菜单开始====================*/ //该树状菜单由catge原创,有任何疑问和BUG请联系QQ:965496 Blog:catge.blogbus.com //该菜单支持无限级数,增加级数方法:三级时level赋值5,以后每增加一级level值增2 //该例CSS样式表已经定义到5级样式, //套用该函数只需在树状菜单根节点上使用id="side_menu",列表需使用UL /*===================================================*/ var level = 5; var n = level; //判断该对象是否包含于side_menu中 function isInSideMenu(obj){ if(n){ n--; if(obj.getAttribute&&obj.getAttribute("id")=="side_menu"){n = level;return true;} if(obj.nodeType==9){n = level;return false;}//超过当前文档树结构时返回false else{return isInSideMenu(obj.parentNode);} } n = level; return false; } //菜单折叠效果函数 function foldMenu(){ if(this.nextSibling&&this.nextSibling.nextSibling){ if(this.nextSibling.nextSibling.style.display=="block"){ this.nextSibling.nextSibling.style.display ="none"; this.innerHTML = this.innerHTML.replace("-","+"); }else{ this.nextSibling.nextSibling.style.display ="block"; this.innerHTML = this.innerHTML.replace("+","-"); } }else{ this.innerHTML = this.innerHTML.replace("+","-"); } } //初始化树状菜单 function intSideMenu(){ var as = document.getElementsByTagName("a"); var uls = document.getElementsByTagName("ul"); var sidemenuUL = document.getElementById("side_menu"); for(var i=0;i<as.length;i++){ if(isInSideMenu(as[i])){ as[i].onclick= foldMenu; as[i].innerHTML = as[i].innerHTML.replace("-","+"); } }//给所有树状菜单内的a绑定foldMenu函数 for(var j=0;j<uls.length;j++){ if(isInSideMenu(uls[j]))uls[j].style.display ="none";//初始化隐藏树状菜单 } sidemenuUL.style.display = "block";//显示根菜单 } intSideMenu(); /*====================//树状菜单结束==========================*/ </script> </html>