本例是應用別人的例子
<!DOCTYPE html PUBLIC "
<html xmlns="
<head>
<title>很實用的JS+CSS多級樹形展開菜單</title>
<style type="text/css">
body{margin:
ul
ul{list
a{text
#root{margin:
#root li{line
#root
#root
#root
#root li a{color:#
#root
</style>
</head>
<body>
<ul id="root">
<li>
<label><a href="javascript:;">校訊通</a></label>
<ul class="two">
<li>
<label><a href="javascript:;">沈陽市</a></label>
<ul class="two">
<li>
<label><a href="javascript:;">二小</a></label>
<ul class="two">
<li><label><a href="javascript:;">二年級</a></label></li>
<li>
<label><a href="javascript:;">三年級</a></label>
<ul class="two">
<li>
<label><a href="javascript:;">一班</a></label>
<ul class="two">
<li><label><a href="javascript:;">張三</a></label></li>
<li>
<label><a href="javascript:;">王五</a></label>
<ul class="two">
<li><label><a href="javascript:;">班長</a></label></li>
<li><label><a href="javascript:;">學習委員</a></label></li>
</ul>
</li>
</ul>
</li>
<li><label><a href="javascript:;">實驗班</a></label></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<label><a href="javascript:;">撫順市</a></label>
<ul class="two">
<li><label><a href="javascript:;">二小</a></label></li>
<li><label><a href="javascript:;">一中</a></label></li>
</ul>
</li>
</ul>
</li>
</ul>
<script type="text/javascript" >
/**一般JS方法
function addEvent(el
if(el
return el
}
function nextnode(node){//尋找下一個兄弟並剔除空的文本節點
if(!node)return ;
if(node
return node;
if(node
return nextnode(node
}
function prevnode(node){//尋找上一個兄弟並剔除空的文本節點
if(!node)return ;
if(node
return node;
if(node
return prevnode(node
}
addEvent(document
e = e||window
var target = e
var tp = nextnode(target
switch(target
case
if(tp&&tp
if(tp
tp
prevnode(target
}else{
tp
prevnode(target
}
}
break;
case
var ap = nextnode(nextnode(target
if(ap
ap
target
}else{
ap
target
}
break;
}
});
window
var labels = document
for(var i=
var span = document
span
span
span
if(nextnode(labels[i]
labels[i]
else
labels[i]
}
}
**/
//面向對象方法
var Tree = function(o){
this
this
var that = this;
this
Tree
}
Tree
int:function(){//初始化頁面
for(var i=
var span = document
span
span
span
if(this
this
else
this
}
}
treeshow:function(e){
e = e||window
var target = e
var tp = this
switch(target
case
if(tp&&tp
if(tp
tp
this
}else{
tp
this
}
}
break;
case
var ap = this
if(ap
ap
target
}else{
ap
target
}
break;
}
}
addEvent:function(el
if(el
return el
}
nextnode:function(node){//尋找下一個兄弟並剔除空的文本節點
if(!node)return ;
if(node
return node;
if(node
return this
}
prevnode:function(node){//尋找上一個兄弟並剔除空的文本節點
if(!node)return ;
if(node
return node;
if(node
return prevnode(node
}
}
tree = new Tree("root");//實例化應用
</script>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20642.html