无级导航 但必须有根节点
<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>
<!--1、保证用户在禁用JS时也能正常显示改菜单,所以使用的是标准的UL和DOM,已经通过测试的有IE7和FIREFOX,其他浏览器不知道。
2、intSideMenu();进行一个菜单初始化的行为。给ul id="side_menu"里的A绑定onclick事件,同时把从最外层的挂钩UL到最里层的UL隐藏,并将[-]改成[+]做成树状效果。
3、isInSideMenu()函数判断传入的对象是否为ul id="side_menu"包含的元素,通过这个函数过滤掉外面的干扰UL和A,这个函数很短,但很巧妙,感觉像是自己偶然的收获。
4、foldMenu()函数对前面绑定了onclick的a进行不同的效果呈现,前面几个if就是用来区分不同的A。该函数比较依赖页面结构,所以如果要改进,这个函数应该改的比较多,不过现在看来这种效果已经足够。-->
三级
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript实现树状(树形)三级伸缩菜单(组合式)</title>
<meta name="author" content="叶随风">
<meta name="author’s Email" content="[email protected]" />
<meta name="author’s QQ" content="19855466"></head>
<style type="text/css">
#js_title{font-size:16px; font-weight:bold; padding:5px 0px;}
#js_describe{font-size:14px; padding:10px 0px;}
div{font-size:14px; height:100%; line-height:200%;}
</style>
<script type="text/javascript">
function showList(div_id)
{
var obj = document.getElementById(div_id + "_ljf");
var click_obj = document.getElementById(div_id);
var text = click_obj.innerHTML;
if(obj.style.display == "none")
{
var str = div_id.substring(0,div_id.length - parseInt(getNumberLen(div_id)));
var num = div_id.replace(str,"");
for(i=1;;i++) //二级可去掉
{
if(!document.getElementById(str+i)) break;
document.getElementById(str+i+"_ljf").style.display = "none";
var iHtml = document.getElementById(str+i).innerHTML;
}
obj.style.display = "block";
}
else
{
obj.style.display = "none";
}
}
function getNumberLen(str)
{
var ljf = null;
if (ljf = str.match(/(/d+)[^/d]*$/))
{
return ljf[1].length;
}
}
</script>
</head>
<body>
<div id="js_title">JavaScript实现树状(树形)三级伸缩菜单(组合式)</div>
<div id="js_describe">功能描述:点击一个选项显示其下属项列表,再次点击收起;其他项列表收起</div>
<div id="pNode1" onclick="showList(this.id);" style="cursor:pointer;">+ 网址导航</div>
<div id="pNode1_ljf" style="display:none;">
<div id="pNode1_sub1" onclick="showList(this.id);" style="padding-left:20px; cursor:pointer;">+ 学习网站</div>
<div id="pNode1_sub1_ljf" style="display:none;">
<div id="pNode1_sub1_s1" style="padding-left:40px;">·<a href="http://www.popasp.com/" target="_blank">泡泡ASP</a></div>
<div id="pNode1_sub1_s2" style="padding-left:40px;">·<a href="http://www.csdn.net/" target="_blank">CSDN</a></div>
</div>
<div id="pNode1_sub2" onclick="showList(this.id);" style="padding-left:20px; cursor:pointer;">+ 导航站点</div>
<div id="pNode1_sub2_ljf" style="display:none;">
<div id="pNode1_sub2_s1" style="padding-left:40px;">·<a href="http://www.popasp.com/" target="_blank">名客</a></div>
<div id="pNode1_sub2_s2" style="padding-left:40px;">·<a href="http://www.hao123.com/" target="_blank">hao123</a></div>
</div>
</div>
<div id="pNode2" onclick="showList(this.id);" style="cursor:pointer;">+ 第二组</div>
<div id="pNode2_ljf" style="display:none;">
<div id="pNode2_sub1" onclick="showList(this.id);" style="padding-left:20px; cursor:pointer;">+ 第二组1</div>
<div id="pNode2_sub1_ljf" style="display:none;">
<div id="pNode2_sub1_s1" style="padding-left:40px;">·第二组1-1</div>
<div id="pNode2_sub1_s2" style="padding-left:40px;">·第二组1-2</div>
<div id="pNode2_sub1_s3" style="padding-left:40px;">·第二组1-3</div>
</div>
<div id="pNode2_sub2" onclick="showList(this.id);" style="padding-left:20px; cursor:pointer;">+ 第二组2</div>
<div id="pNode2_sub2_ljf" style="display:none;">
<div id="pNode2_sub2_s1" style="padding-left:40px;">·第二组2-1</div>
<div id="pNode2_sub2_s2" style="padding-left:40px;">·第二组2-2</div>
<div id="pNode2_sub2_s3" style="padding-left:40px;">·第二组2-3</div>
</div>
</div>
<div id="pNode3" onclick="showList(this.id);" style="cursor:pointer;">+ 第三组</div>
<div id="pNode3_ljf" style="display:none;">
<div id="pNode3_sub1" onclick="showList(this.id);" style="padding-left:20px; cursor:pointer;">+ 第三组1</div>
<div id="pNode3_sub1_ljf" style="display:none;">
<div id="pNode3_sub1_s1" style="padding-left:40px;">·第三组1-1</div>
<div id="pNode3_sub1_s2" style="padding-left:40px;">·第三组1-2</div>
<div id="pNode3_sub1_s3" style="padding-left:40px;">·第三组1-3</div>
</div>
<div id="pNode3_sub2" onclick="showList(this.id);" style="padding-left:20px; cursor:pointer;">+ 第三组2</div>
<div id="pNode3_sub2_ljf" style="display:none;">
<div id="pNode2_sub3_s1" style="padding-left:40px;">·第三组2-1</div>
<div id="pNode2_sub3_s2" style="padding-left:40px;">·第三组2-2</div>
<div id="pNode2_sub3_s3" style="padding-left:40px;">·第三组2-3</div>
</div>
</div>
</body>
</html>
无需根节点导航列表
<table valign="top" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFCCFF">
<tr>
<td><img src="images/m01.jpg" border="0" width="179" height="52" /></td>
</tr>
<tr>
<td valign="top" bgcolor="#ffffff">
<a onClick="Javascript:if (d1.style.display=='none'){d1.style.display='';}else {d1.style.display='none';};">
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<td ><img src="images/n02.jpg" width="179" height="41" /></td>
</tr>
</table>
</a>
<div id="d1" style="DISPLAY:none;list-style-type:none;margin:0; padding:0;list-style:none; ">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#f5f5f5">
<tr>
<td height="22" ><a href="culture!append.action"><img border="0" src="images/n01.jpg" width="179" height="35" /></a></td>
</tr>
<tr>
<td height="22"><a href="culture.action"><img border="0" src="images/n03.jpg" width="179" height="35" /></a></td>
</tr>
</table>
</div>