本例改編自他人寫的無限級折疊菜單,在此先向原作者致敬!在原有基礎上,我做了一些簡化和重構,把原本用普通方法實現的菜單改寫成了面向對象的方式。示例中展開與收合的小圖標可以自行替換,方便查看更好的視覺效果。
完整的示例代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>很實用的JS+CSS多級樹形展開菜單</title>
<style type="text/css">
body{margin:0;padding:0;font:12px/1.5 Tahoma,Helvetica,Arial,sans-serif;}
ul,li,{margin:0;padding:0;}
ul{list-style:none;}
a{text-decoration: none;}
#root{margin:10px;width:200px;overflow:hidden;}
#root li{line-height:25px;}
#root .rem{padding-left:16px;}
#root .add{background:url(treeico.gif) -4px -31px no-repeat;}
#root .ren{background:url(treeico.gif) -4px -7px no-repeat;}
#root li a{color:#666666;padding-left:5px;outline:none;blr:expression(this.onFocus=this.blur());}
#root .two{padding-left:20px;display:none;}
</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" >
//面向對象方法
var Tree = function(o){
this.root = document.getElementById(o);
this.labels = this.root.getElementsByTagName('label');
var that = this;
this.int();
Tree.prototype.addEvent(this.root,'click',function(e){that.treeshow(e)});
}
Tree.prototype = {
int:function(){//初始化頁面,加載時給有孩子結點的元素動態添加圖標
for(var i=0;i<this.labels.length;i++){
var span = document.createElement('span');
span.style.cssText ='display:inline-block;height:18px;vertical-align:middle;width:16px;cursor:pointer;';
span.innerHTML = ' ';
span.className = 'add';
if(this.nextnode(this.labels[i].nextSibling)&&this.nextnode(this.labels[i].nextSibling).nodeName == 'UL')
this.labels[i].parentNode.insertBefore(span,this.labels[i]);
else
this.labels[i].className = 'rem'
}
},
treeshow:function(e){
e = e||window.event;
var target = e.target||e.srcElement;
var tp = this.nextnode(target.parentNode.nextSibling);
switch(target.nodeName){
case 'A'://點擊A標簽展開和收縮樹形目錄,並改變其樣式
if(tp&&tp.nodeName == 'UL'){
if(tp.style.display != 'block' ){
tp.style.display = 'block';
this.prevnode(target.parentNode.previousSibling).className = 'ren'
}else{
tp.style.display = 'none';
this.prevnode(target.parentNode.previousSibling).className = 'add'
}
}
break;
case 'SPAN'://點擊圖標只展開或者收縮
var ap = this.nextnode(this.nextnode(target.nextSibling).nextSibling);
if(ap.style.display != 'block' ){
ap.style.display = 'block';
target.className = 'ren'
}else{
ap.style.display = 'none';
target.className = 'add'
}
break;
}
},
addEvent:function(el,name,fn){//綁定事件
if(el.addEventListener) return el.addEventListener(name,fn,false);
return el.attachEvent('on'+name,fn);
},
nextnode:function(node){//尋找下一個兄弟並剔除空的文本節點
if(!node)return ;
if(node.nodeType == 1)
return node;
if(node.nextSibling)
return this.nextnode(node.nextSibling);
},
prevnode:function(node){//尋找上一個兄弟並剔除空的文本節點
if(!node)return ;
if(node.nodeType == 1)
return node;
if(node.previousSibling)
return this.prevnode(node.previousSibling);
}
}
tree = new Tree("root");//實例化應用
</script>
</body>
</html>
相比原本零散的普通函數寫法,面向對象的重構把事件綁定、節點查找、初始化等邏輯都封裝進了 Tree 類的原型方法中,結構更清晰,也更容易維護和擴展。通過 new Tree("root") 一行就能完成實例化,調用起來乾淨利落。