打造完美的JS树形菜单

其实网上现在用JS来实现一个树形的代码多得不成样子了.不过偶觉得这个还算可以的(至少比我第一次写的那个强多了o_o)

我在WEB中JS初始化如下:

None.gif <  script language  =   "  javascript  "   >  
None.gifShow_Root  
=     2  
None.gifInsert_ID  
=     "  content  "  
None.gifTarget_Text  
=     "  ContentCortrol  "  
None.gifRoot_Name  
=     "  n0  "  
None.gif
var  myurl   =     "  http://www.blogjava.net/jkallen  "  ;
None.gif
None.gif 
//  我以当节点处于1, 4 ,7的时候作为父节点处理
None.gif
var  n0   =     new   Node(  "  n0  "  ,  "  节点0  "  ,myurl,  "  none  "  );
None.gif
var  n1   =   n0.createChild(  "  n1  "  ,  "  节点1  "  ,myurl,  "  none  "  )    ;    
None.gif
var  n2   =   n1.createChild(  "  n2  "  ,  "  节点2  "  ,myurl,  "  none  "  )    ;
None.gif
var  n3   =   n1.createChild(  "  n3  "  ,  "  节点3  "  ,myurl,  "  none  "  )    ;
None.gif
var  n4   =   n0.createChild(  "  n4  "  ,  "  节点4  "  ,myurl,  "  none  "  )    ;
None.gif
var  n5   =   n4.createChild(  "  n5  "  ,  "  节点5  "  ,myurl,  "  none  "  )    ;
None.gif
var  n6   =   n4.createChild(  "  n6  "  ,  "  节点6  "  ,myurl,  "  none  "  )    ;
None.gif
var  n7   =   n0.createChild(  "  n7  "  ,  "  节点7  "  ,myurl,  "  none  "  )    ;
None.gif
var  n8   =   n7.createChild(  "  n8  "  ,  "  节点8  "  ,myurl,  "  none  "  )    ;
None.gif
var  n9   =   n0.createChild(  "  n9  "  ,  "  节点9  "  ,myurl,  "  none  "  )    ;
None.gif
None.gifloadPage( 
"  n0  "  )
None.gif 
</  script  >  

看起来相当笨拙.但这仅是为了表现JS罢了,其实各种node完全可以在bean中实现才是(node value也正是从DB是取才是啊)!

先来看效果啦:


o_menu.jpg

其中一些主要的JS代码分别如下:
Node的定义:

None.gif   function   Node(name, text, link, status)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
this .name   =   name
InBlock.gif    
this .text   =   text
InBlock.gif    
this .link   =   link
InBlock.gif    
this .status   =   status
InBlock.gif    
InBlock.gif    
this .depth   =     0  
InBlock.gif    
this .order   =     1  
InBlock.gif    
this .parent   =     ""  
InBlock.gif    
this .folder   =     0  
InBlock.gif    
this .closed   =     1  
InBlock.gif            
InBlock.gif    
this .childCount   =     0  
InBlock.gif    
this .childArray   =     new    Array ()
InBlock.gif    
InBlock.gif    
this .isRoot   =   isRoot
InBlock.gif    
this .isFolder   =   isFolder
InBlock.gif    
this .isClosed   =   isClosed
InBlock.gif    
this .isHeadChild   =   isHeadChild
InBlock.gif    dot.gif  dot.gif..
ExpandedBlockEnd.gif     }
 


产生子节点:

None.gif   function   expandChild(iRoot)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var  sOutput   =    this .getMyBranch(iRoot)
InBlock.gif    sOutput  
=     "  <tr bgcolor='#FFFFFF'><td class='text' width=260>  "     +   sOutput
InBlock.gif     
if   (iRoot)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        iDepth  
=     0  
ExpandedSubBlockEnd.gif    }

InBlock.gif     
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        iDepth  
=     1  
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif     
if   ( !   this .isFolder())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        sOutput  
+=   getImageStr(  3  )   +     "  <a href='  "     +    this .link   +     "  ' target='  "     +   Target_Text   +     "  '>  "  
InBlock.gif                 
+     "  <span id='  "     +    this .name   +     "  ' class='node' οnclick=changeColor('  "     +    this .name   +     "  ')>  "  
InBlock.gif                 
+    this .text   +     "  </span></a></td>  "  
ExpandedSubBlockEnd.gif    }

InBlock.gif     
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
if   ( this .depth   >=   iDepth)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var  iIconID   =     0  
InBlock.gif             
if   ( this .isLastChild()) iIconID   =     1  
InBlock.gif             
if   ( this .depth   ==   iDepth)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 
if   ( this .isHeadChild()) iIconID   =     2  
InBlock.gif                 
if   ( this .isSingleton()) iIconID   =     3  
ExpandedSubBlockEnd.gif            }

InBlock.gif                sOutput  
+=     "  <a href='JavaScript:  "     +    this .name   +     "  .changeState()'>  "  
InBlock.gif                     
+   getImageStr(  1  ,  this .isClosed()   *     4     +   iIconID)   +     "  </a>  "  
InBlock.gif            sOutput  
+=   getImageStr(  2  ,  this .isClosed())
InBlock.gif                    
InBlock.gif             
if   ( this .link   ==     ""  )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 
//  sOutput  +=   " <a href='JavaScript: "   +  this.name  +   " .changeState()'> " 
ExpandedSubBlockEnd.gif
            }

InBlock.gif             
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sOutput  
+=     "  <a href='  "     +    this .link   +     "  ' target='  "     +   Target_Text   +     "  '>  "  
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            sOutput  
+=     "  <span id='  "     +    this .name   +     "  ' class='node'  "  
InBlock.gif             
if   ( this .link   ==     ""  )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sOutput  
+=     "  >  "  
ExpandedSubBlockEnd.gif            }

InBlock.gif             
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sOutput  
+=     "   οnclick=changeColor('  "     +    this .name   +     "  ')>  "  
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif            sOutput  
+=    this .text   +     "  </span></a></td>  "     +     "  <td width=40>  "     +   getStatusImageStr( this )   +     "  </td>  "          }

InBlock.gif        sOutput  
=   sOutput   +     "  </tr>  "  
InBlock.gif         
if   ( !   this .isClosed())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             
for   ( var  i   =     0  ; i  <    this .childCount; i   ++  )
InBlock.gif                sOutput  
+=    this .childArray[i].expandChild(iRoot)
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return  sOutput
ExpandedBlockEnd.gif}
 


展开父节点:

None.gif   function   expandChild(iRoot)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var  sOutput   =    this .getMyBranch(iRoot)
InBlock.gif    sOutput  
=     "  <tr bgcolor='#FFFFFF'><td class='text'>  "     +   sOutput
InBlock.gif     
if   (iRoot)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        iDepth  
=     0  
ExpandedSubBlockEnd.gif    }

InBlock.gif     
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        iDepth  
=     1  
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif     
if   ( !   this .isFolder())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        sOutput  
+=   getImageStr(  3  )   +     "  <a href='  "     +    this .link   +     "  ' target='  "     +   Target_Text   +     "  '>  "  
InBlock.gif                 
+     "  <span id='  "     +    this .name   +     "  ' class='node' οnclick=changeColor('  "     +    this .name   +     "  ')>  "  
InBlock.gif                 
+    this .text   +     "  </span></a></td>  "  
ExpandedSubBlockEnd.gif    }

InBlock.gif     
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
if   ( this .depth   >=   iDepth)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var  iIconID   =     0  
InBlock.gif             
if   ( this .isLastChild()) iIconID   =     1  
InBlock.gif             
if   ( this .depth   ==   iDepth)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 
if   ( this .isHeadChild()) iIconID   =     2  
InBlock.gif                 
if   ( this .isSingleton()) iIconID   =     3  
ExpandedSubBlockEnd.gif            }

InBlock.gif                sOutput  
+=     "  <a href='JavaScript:  "     +    this .name   +     "  .changeState()'>  "  
InBlock.gif                     
+   getImageStr(  1  ,  this .isClosed()   *     4     +   iIconID)   +     "  </a>  "  
InBlock.gif            sOutput  
+=   getImageStr(  2  ,  this .isClosed())
InBlock.gif                    
InBlock.gif             
if   ( this .link   ==     ""  )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 
//  sOutput  +=   " <a href='JavaScript: "   +  this.name  +   " .changeState()'> " 
ExpandedSubBlockEnd.gif
            }

InBlock.gif             
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sOutput  
+=     "  <a href='  "     +    this .link   +     "  ' target='  "     +   Target_Text   +     "  '>  "  
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            sOutput  
+=     "  <span id='  "     +    this .name   +     "  ' class='node'  "  
InBlock.gif             
if   ( this .link   ==     ""  )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sOutput  
+=     "  >  "  
ExpandedSubBlockEnd.gif            }

InBlock.gif             
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sOutput  
+=     "   οnclick=changeColor('  "     +    this .name   +     "  ')>  "  
ExpandedSubBlockEnd.gif            }

InBlock.gif            sOutput  
+=    this .text   +     "  </span></a></td>  "  
ExpandedSubBlockEnd.gif        }

InBlock.gif        sOutput  
=   sOutput   +     "  </tr>  "  
InBlock.gif         
if   ( !   this .isClosed())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             
for   ( var  i   =     0  ; i  <    this .childCount; i   ++  )
InBlock.gif                sOutput  
+=    this .childArray[i].expandChild(iRoot)
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return  sOutput
ExpandedBlockEnd.gif}
 


注意:

从以上可以看到在WEB页面中得有相关JS代码初始化Insert_ID(create space to storage the menu) 如:

None.gif < table  width ="100%" >
None.gif
< tr >
None.gif 
< td  valign ="top"  id ="content"  nowrap >
None.gif 
</ td >
None.gif
</ tr >
None.gif
</ table >  
None.gif
None.gifInsert_ID = "content"//JS代码 
None.gif
None.gif

 

在WEB页面中得初始化Root_Name(此处为"n0")

JS中没有提供对父节点的判断,而是直接(依据当前节点的下标)取得某一节点的父节点(反正菜单项一般是从DB中select的,所以你可以在bean中去处理).我觉得也许可以将此功能也在JS中实现,将DB中的数据直接提到XML(or我们JS是从JS中提取数据的)....

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值