javascript动态生成树形菜单的方法

本文实例讲述了javascript动态生成树形菜单的方法。分享给大家供大家参考,具体如下:

//========================================
//Envrionment to hold Listeners
//========================================
tv_listeners = new Array() ;
function listener( type , handler )  {
 this.type = type ;
 this.handler = handler ;
 this.id = tv_listeners.length ;
 tv_listeners[ tv_listeners.length ] = this ;
}
function addListener( type , handler ) {
  new listener( type , handler ) ;
 }
//=== END =====
//=========================================
// Hold the top item
//=========================================
tv_topnodeitem = null ;
//===== END =======
//=========================================
//Hold nodeitems , and supply a nodeitem Register
//=========================================
nodeitems = new Array() ;
function nodeitemRegister( obj )  {
  nodeitems[ nodeitems.length ] = obj ;
 return nodeitems.length - 1 ;
}
//=== END =======
//=================================
//Custom a stack
//Class     : stack
//metheds  : get()
//          put( obj )
//=================================
function stack() {
  this.value = new Array() ;
 this.cursor = 0 ;
}
function stack_get() {
   this.cursor = this.cursor - 1 ;
 return this.value[ this.cursor ] ;
}
function stack_put( obj )  {
   this.value[ this.cursor ] = obj ;
   this.cursor = this.cursor + 1 ;
}
stack.prototype.get = stack_get ;
stack.prototype.put = stack_put ;
//=======END ==========
//=========================================
// Define a public stack
//=========================================
userstack = new stack() ;
//====== END ===========
//=========================================
//Image List
//=========================================
treeview_box_0_none = "../images/4_clos.gif" ;
treeview_box_0_line = "../images/4_none.gif" ;
treeview_box_2_open = "../images/2_open.gif" ;
treeview_box_2_none = "../images/2_none.gif" ;
treeview_box_2_close = "../images/2_clos.gif" ;
treeview_box_1_open = "../images/3_open.gif" ;
treeview_box_1_none = "../images/3_none.gif" ;
treeview_box_1_close = "../images/3_clos.gif" ;
//===============================================
//Class : nodeitem
//status------------------------1:two-direction    0:nobox    0: disactivite
//               2:three-0direction   1:close-box  1: activite
//                          2:open-box
//===============================================
function nodeitem( parentkey , key , lable , img ) {
  this.lable = lable ;
 this.key = key ;
  this.parent = findNode( parentkey ) ;
 if( this.parent != null ) {
   aa = this.parent.status ;
   if( aa.substring( 1 , 2 ) == "0" )
     this.parent.status = aa.substring( 0 , 1 ) + "1" + aa.substring( 2 , 3 ) ;
   if( this.parent.maxsubitem != null )
     this.parent.maxsubitem.status = "2" + this.parent.maxsubitem.status.substring( 1 , 3 ) ;
    this.parent.subitems[ this.parent.subitems.length ] = this ;
    this.parent.maxsubitem = this ;
  }
  else {
   if( tv_topnodeitem != null )  {
    alert( "不能有两个顶项!" ) ;
  return ;
    }
    tv_topnodeitem = this ;
  }
 this.img = img ;
 this.tag = null ;
  this.status = "100" ;
 this.subitems = new Array() ;
 this.maxsubitem = null ;
  this.id = nodeitemRegister( this ) ;
 //**********************
 this.questionId = 0;
 this.description = "";
 //this.url = null;
 //**********************
 //added by msb for the sort and move up/down
 /*if ( this == tv_topnodeitem )
 {
 this.nodeIndex = 0;
 } else {
 this.nodeIndex = this.parent.subitems.length;
 }*/
 //end added
}
//added by msb for the sort and move up/down
function nodeitem_moveUp() {
 if (this == tv_topnodeitem) return; //topitem
 ssubitems = this.parent.subitems;
 for ( i=0; i<ssubitems.length; i++ ) {
 if( ssubitems[i] == this ) {
  break;
 }
 }
 if (i==0) return;
 ssubitems[i] = ssubitems[i-1];
 ssubitems[i-1] = this;
 if (i==ssubitems.length-1) {
 ssubitems[i-1].status = "2" + ssubitems[i-1].status.substring(1, 3);
 ssubitems[i].status = "1" + ssubitems[i].status.substring(1, 3);
 }
 /*
 itemTemp = this;
 ssubitems[this.nodeIndex-1] */
/* for ( i=0; i<ssubitems.length; i++ ) {
 if( ssubitems[i] != null && ssubitems[i].nodeIndex == (this.nodeIndex-1) )
  previousitem = ssubitems[i]
 }
 previousitem.nodeIndex = this.nodeIndex;
 this.nodeIndex = this.nodeIndex -1;
 swap(this,previousitem);
*/
 //label_on_click(this.id);
 this.parent.refresh();
 lable_on_click(this.id);
}//moveUp()
function nodeitem_moveDown() {
 if (this == tv_topnodeitem) return; //topitem
 ssubitems = this.parent.subitems;
 for ( i=0; i<ssubitems.length; i++ ) {
 if( ssubitems[i] == this ) {
  break;
 }
 }
 if (i==ssubitems.length-1) return;
 ssubitems[i] = ssubitems[i+1];
 ssubitems[i+1] = this;
 if (i==ssubitems.length-2) {
 ssubitems[i+1].status = "1" + ssubitems[i+1].status.substring(1, 3);
 ssubitems[i].status = "2" + ssubitems[i].status.substring(1, 3);
 }
 this.parent.refresh();
 lable_on_click(this.id);
}//moveDown()
/*function swap (item1, item2) {
 nodeitems[item1.id] = item2;
 nodeitems[item2.id] = item1;
 idTemp = item1.id;
 item1.id = item2.id;
 item2.id = idTemp;
}*/
//end added
function nodeitem_setTag( obj ) {
  this.tag = obj ;
}
function nodeitem_getTag() {
  return this.tag ;
}
function nodeitem_show() {
 str = "<span id = 'preface" + this.id + "'><table border='0' cellspacing='0' cellpadding='0'><tr><td>" ;
  str_f = "" ;
  for( j = this.parent ; j != null ; j = j.parent ) {
   if( j.status.substring( 0 , 1 ) == 1 )
   str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ;
    else
   str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ;
  }
  str = str + str_f ;
  str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ;
  switch( this.status.substring( 0 , 2 ) )  {
    case "10" : str += treeview_box_1_none ; break ;
    case "11" : str += treeview_box_1_close ; break ;
    case "12" : str += treeview_box_1_open ; break ;
    case "20" : str += treeview_box_2_none ; break ;
    case "21" : str += treeview_box_2_close ; break ;
    case "22" : str += treeview_box_2_open ; break ;
  }
  str += "' align='absmiddle' onclick='box_on_click(this)'>" ;
  if( this.img == "" )
   str += this.img ;
  else
   str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ;
  str += "</td><td><table border='0' cellspacing='1' cellpadding='1' style='font-size:9pt; color:#333333' id='lablePanel" + this.id + "'><tr><td ondblclick = 'lable_on_dblclick(" + this.id + ")' onclick='lable_on_click(" + this.id + ")' style='cursor:hand' id='f_lablePanel" + this.id + "' nowrap>" + this.lable + "</td></tr></table></td></tr></table>" ;
 str += "</span><span id = 'tv_panel_" + this.id + "' style='display:" ;
 if( this.status.substring( 1 , 2 ) == '2' )
  str += "" ;
 else
  str += "none" ;
  str += "'></span>" ;
 if( this.parent == null )
   for(var i in document.all){
     if (document.all[i].tagName == "BODY")
     {
    document.all[i].insertAdjacentHTML( "AfterBegin" , str ) ;
       break
     }
    }
 else
 document.all( "tv_panel_" + this.parent.id ).insertAdjacentHTML( "BeforeEnd" , str ) ;
  for( m = 0 ; m < this.subitems.length ; m ++ )
  if( this.subitems[ m ] != null ) {
   userstack.put( m ) ;
   this.subitems[ m ].show() ;
   m = userstack.get() ;
  }
}
function nodeitem_refresh() {
 str = "<table border='0' cellspacing='0' cellpadding='0'><tr><td>" ;
  str_f = "" ;
  for( j = this.parent ; j != null ; j = j.parent ) {
   if( j.status.substring( 0 , 1 ) == 1 )
   str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ;
    else
   str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ;
  }
 str = str + str_f ;
  str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ;
  switch( this.status.substring( 0 , 2 ) )  {
     case "10" : str += treeview_box_1_none ; break ;
     case "11" : str += treeview_box_1_close ; break ;
     case "12" : str += treeview_box_1_open ; break ;
     case "20" : str += treeview_box_2_none ; break ;
     case "21" : str += treeview_box_2_close ; break ;
     case "22" : str += treeview_box_2_open ; break ;
  }
  str += "' align='absmiddle' onclick='box_on_click(this)'>" ;
  if( this.img == "" )
   str += this.img ;
  else
   str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ;
  str += "</td><td><table border='0' cellspacing='1' cellpadding='1' style='font-size:9pt; color:#333333' id='lablePanel" + this.id + "'><tr><td ondblclick = 'lable_on_dblclick(" + this.id + ")' onclick='lable_on_click(" + this.id + ")' style='cursor:hand' id='f_lablePanel" + this.id + "' nowrap>" + this.lable + "</td></tr></table></td></tr></table>" ;
 document.all( "preface" + this.id ).innerHTML = str ;
  document.all( "tv_panel_" + this.id ).innerHTML = "" ;
 for( m = 0 ; m < this.subitems.length ; m ++ )
  if( this.subitems[ m ] != null ) {
   userstack.put( m ) ;
   this.subitems[ m ].show() ;
   m = userstack.get() ;
    }
}
function nodeitem_remove() {
  pparent = this.parent ;
 if( pparent == null )  {
    removenodeitem( this.id ) ;
    for(var i in document.all){
      if (document.all[i].tagName == "BODY")
       {
   document.all[i].innerHTML = "" ;
        break
       }
     }
 return ;
 }
 lastsubitem = null ;
 for( i = 0 ; i < pparent.subitems.length ; i ++ )
  if( pparent.subitems[ i ] != null )
   if ( pparent.subitems[ i ] == this )
     pparent.subitems[ i ] = null ;
      else
     lastsubitem = pparent.subitems[ i ] ;
  pparent.maxsubitem = lastsubitem ;
 if( lastsubitem == null )
    pparent.status = pparent.status.substring( 0 , 1 ) + "0" + pparent.status.substring( 2 , 3 ) ;
  else
   pparent.maxsubitem.status = "1" + pparent.maxsubitem.status.substring( 1 , 3 ) ;
   removenodeitem( this.id ) ;
 //added by msb for move up/down
 arrTemp = new Array();
 j = 0;
 for ( i=0; i<pparent.subitems.length; i++ ) {
 if ( pparent.subitems[i] != null ) {
  arrTemp[j] = pparent.subitems[i];
  j++;
 }
 }
 this.parent.subitems = arrTemp;
 //end added
 pparent.refresh() ;
 //tv_topnodeitem.refresh() ;
}
function removenodeitem( id )  {
  curitem = nodeitems[ id ] ;
 nodeitems[ id ] = null ;
 for( m = 0 ; m < curitem.subitems.length ; m ++ )
   if( curitem.subitems[ m ] != null )  {
   userstack.put( m ) ;
   removenodeitem( curitem.subitems[ m ].id ) ;
   m = userstack.get() ;
    }
}
function nodeitem_boxclick() {
   if( this.status.substring( 1 ,2 ) == "0" )
    return ;
 if( this.status.substring( 1 ,2 ) == "1" )
    this.open() ;
 else
    this.close() ;
}
function nodeitem_close() {
   this.status = this.status.substring( 0 , 1 ) + "1" + this.status.substring( 2 , 3 ) ;
   document.all( "tv_panel_" + this.id ).style.display = "none" ;
   eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_close" ) ;
}
function nodeitem_open() {
   this.status = this.status.substring( 0 , 1 ) + "2" + this.status.substring( 2 , 3 ) ;
   document.all( "tv_panel_" + this.id ).style.display = "" ;
   eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_open" ) ;
}
//added by msb for the move up/down
nodeitem.prototype.moveUp = nodeitem_moveUp;
nodeitem.prototype.moveDown = nodeitem_moveDown;
//end added
nodeitem.prototype.show = nodeitem_show ;
nodeitem.prototype.refresh = nodeitem_refresh ;
nodeitem.prototype.boxclick = nodeitem_boxclick ;
nodeitem.prototype.close = nodeitem_close ;
nodeitem.prototype.open = nodeitem_open ;
nodeitem.prototype.remove = nodeitem_remove ;
nodeitem.prototype.setTag = nodeitem_setTag ;
nodeitem.prototype.getTag = nodeitem_getTag ;
//==========================================================
// Public Methods
//==========================================================
function showTV() {
   tv_topnodeitem.show() ;
}
function findNode( key )  {
 pppp = null;
  for( i = 0 ; i < nodeitems.length ; i ++ ) {
  if( nodeitems[ i ] != null ) {
    if( nodeitems[ i ].key == key ) {
     pppp = nodeitems[ i ] ;
  }
  }
 }
  return pppp ;
}
function addNode( parentkey , key , lable , img )  {
  return new nodeitem( parentkey , key , lable , img ) ;
}
function deleteNode( key )  {
  curNode = findNode( key ) ;
 if( curNode == null )
  return false ;
  curNode.remove() ;
  return true ;
}
//====== END ================
//===========================================================
// Events
//===========================================================
function box_on_click( obj ) {
  nodeitems[ obj.nodeid ].boxclick() ;
}
tv_curlable = null ;
tv_curlable_f = null ;
function lable_on_click( id ) {
    key = nodeitems[ id ].key ;
    if( nodeitems[ id ].parent == null )
    parentkey = "" ;
    else
    parentkey = nodeitems[ id ].parent.key ;
  if( tv_curlable != null ) {
   tv_curlable.bgColor = "transparent" ;
   tv_curlable.style.color = "#333333" ;
   tv_curlable_f.bgColor = "transparent" ;
    }
    tv_curlable = document.all("lablePanel"+id) ;
   tv_curlable.bgColor = "#000000" ;
   tv_curlable.style.color = "#FFFFFF" ;
    tv_curlable_f = document.all("f_lablePanel"+id) ;
   tv_curlable_f.bgColor = "#888888" ;
  for( i = 0 ; i < tv_listeners.length ; i ++ )
    if( tv_listeners[ i ].type == "click" ) {
   h = tv_listeners[ i ].handler ;
   eval( h + "( '" + key + "' , '" + parentkey + "' ) ; " ) ;
      }
}
function lable_on_dblclick( id ) {
 key = nodeitems[ id ].key ;
    if( nodeitems[ id ].parent == null )
    parentkey = "" ;
    else
    parentkey = nodeitems[ id ].parent.key ;
  if( tv_curlable != null ) {
   tv_curlable.bgColor = "transparent" ;
   tv_curlable.style.color = "#333333" ;
   tv_curlable_f.bgColor = "transparent" ;
    }
    tv_curlable = document.all("lablePanel"+id) ;
   tv_curlable.bgColor = "#000000" ;
   tv_curlable.style.color = "#FFFFFF" ;
    tv_curlable_f = document.all("f_lablePanel"+id) ;
   tv_curlable_f.bgColor = "#888888" ;
  for( i = 0 ; i < tv_listeners.length ; i ++ )
    if( tv_listeners[ i ].type == "dblclick" ) {
   h = tv_listeners[ i ].handler ;
   eval( h + "( '" + key + "' , '" + parentkey + "' ) ; " ) ;
      }
}

javascript调用

<script language="javascript">
<!--
 addNode(-1,0,"hehe","../images/top.gif");
 addNode(0,1,"haha","../images/top.gif");
 addNode(0,2,"haha","../images/top.gif");
 addNode(1,3,"haha","../images/top.gif");
 addNode(1,4,"haha","../images/top.gif");
 addNode(2,5,"haha","../images/top.gif");
 addNode(2,6,"haha","../images/top.gif");
 showTV();
-->
</script>

希望本文所述对大家JavaScript程序设计有所帮助。

(0)

相关推荐

  • javascript修改IMG标签的src问题

    1.当点击某一按钮的时候,把图片域中的图片改变一下 复制代码 代码如下: <img id="randimg" src="/servlet/CreateValidateNum" width="60" height="20" /> <span style="cursor:hand" onclick="reflush();return false;">看不清</s

  • JS对img进行操作(换图片/切图/轮换/停止)

    复制代码 代码如下: <script type="text/javascript"> var i = 1; var n; function showImg() { if (document.getElementById('img').getAttribute("src") == "images/1.jpg") { document.getElementById('img').setAttribute("src",&

  • JavaScript实现自动生成网页元素功能(按钮、文本等)

    创建元素的方法: 1.利用createTextNode()创建一个文本对象 2.利用createElement()创建一个标签对象 3.直接利用容器标签中的一个属性:innerHTML-----本质上改该标签容器中的"html代码",不是我们认为的对象树的操作 详解代码: <body> <input type="button" value="创建并添加节点1" onclick="addNode1()"/>

  • JSP结合js实现img中src更新请求的方法

    本文实例讲述了JSP结合js实现img中src更新请求的方法.分享给大家供大家参考.具体如下: 1.javascript(更新的函数) <script type="text/javascript"> function changeImage(){ var img = document.getElementById("imgVcode"); if(img.name == 1){ img.name = 2; img.src = "shop.do?m

  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任意随机数就好理解了.比如实际中我们可能会有如下的需要: (1)生成一个 0 - 100 之间的随机整数,那么则可以: parseInt(100*Math.random()); 注意:因为Math.random()的返回值是包括0和1的,所以这里是有生成0和100的可能性的. (2)生成一个从 m - n 之间的随机整数,例如要生

  • JS+CSS设置img在DIV中只显示Img垂直居中的部分

    一个Div中显示Img,img的宽和Div相同,但高不固定,要求只显示Img垂直居中的部分. 示例: 原图: 代码实现: 复制代码 代码如下: <div style="width: 190px; height: 100px; overflow: hidden;"> <img src="http://beijing.timeoutcn.com/UserFiles/Images/2013-10-21/20131021104840077cover-22-1.jpg

  • javascript生成img标签的3种实现方法(对象、方法、html)

    本文实例讲述了javascript生成img标签的3种实现方法.分享给大家供大家参考,具体如下: <div id="d1"></div> <script> //HTML function a(){ document.getElementById("d1").innerHTML="<img src='http://baike.baidu.com/cms/rc/240x112dierzhou.jpg'>"

  • JS+JSP通过img标签调用实现静态页面访问次数统计的方法

    本文实例讲述了JS+JSP通过img标签调用实现静态页面访问次数统计的方法.分享给大家供大家参考,具体如下: 测试页面: test.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>test</title> <meta http-equiv="pragma" content

  • JSP实现用于自动生成表单标签html代码的自定义表单标签

    本文实例讲述了JSP实现用于自动生成表单标签html代码的自定义表单标签.分享给大家供大家参考.具体如下: 这个是自己写的一个简单的JSP表单标签,用于自动生成checkbox,select,radio等标签,传入菜单集合生成html代码,自动选中指定值,用于java web项目的jsp页面. 1. Servlet部分代码: Map<String, String> map = new HashMap<String, String>(); map.put("2",

  • javascript动态生成树形菜单的方法

    本文实例讲述了javascript动态生成树形菜单的方法.分享给大家供大家参考,具体如下: //======================================== //Envrionment to hold Listeners //======================================== tv_listeners = new Array() ; function listener( type , handler ) { this.type = type ;

  • vue侧边栏动态生成下级菜单的方法

    循环传入的数据去生成下级菜单 <template> <div class="headBar"> <div class="title"> 微商城管理后台 </div> <el-menu class="el-menu-headBar" mode="horizontal" @select="handleSelect" background-color=&quo

  • JavaScript递归算法生成树形菜单

    本文实例为大家分享了js生成树形菜单的具体代码,供大家参考,具体内容如下 1.最终效果图(这里仅为实现算法,并加载至页面,不做任何css界面优化) 注释:本示例包含三级目录菜单,但实际上可支持N级(可使用该代码自行测试) 2.数据源 菜单信息一般来源于数据库中数据表,且为自连接表,其中包含主要字段(主键,菜单名称,父级id): 本示例在前端页面中使用对象数组模拟从数据库获取菜单信息: var menuArry = [ { id: 1, name: "办公管理", pid: 0 }, {

  • asp.net TreeView递归循环子节点生成树形菜单实例

    本文实例讲述了asp.net TreeView递归循环子节点生成树形菜单的方法.分享给大家供大家参考,具体如下: 这里主要用到递归循环获取子结点 /// <summary> /// 生成根节点 /// </summary> /// <param name="treeview"></param> protected void BindTreeView(long ID, TreeView treeview) { DataTable dt =

  • javascript实现在下拉列表中显示多级树形菜单的方法

    本文实例讲述了javascript实现在下拉列表中显示多级树形菜单的方法.分享给大家供大家参考.具体如下: 这里演示在下拉列表框中显示分级的菜单,在很多网站都可以看到的效果,很实用,下拉列表框中的选项是利用JS控制输出,如果你有更好的办法不用JS来显示,那最好了,因为像这种菜单用JS来实现,多多少少有点麻烦. 运行效果截图如下: 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

  • javascript实现树形菜单的方法

    本文实例讲述了javascript实现树形菜单的方法.分享给大家供大家参考.具体如下: var ME={ ini:{i:true,d:{},d1:{},h:0,h1:0,h2:0}, html:function(da,f){ var s='<ul'+(f?' class="f"':'')+'>'; for(var i=0,l=da.length;i<l;i++){ if(typeof(da[i].pid)=='object'){ s+='<li><b

  • javascript 动态生成css代码的两种方法

    javascript 动态生成css代码的两种方法 有时候我们需要利用js来动态生成页面上style标签中的css代码,方法很直接,就是直接创建一个style元素,然后设置style元素里面的css代码,最后把它插入到head元素中.但有些兼容性问题我们需要解决.首先在符合w3c标准的浏览器中我们只需要把要插入的css代码作为一个文本节点插入到style元素中即可,而在IE中则需要利用style元素的styleSheet.cssText来解决.还需要注意的就是在有些版本IE中一个页面上style

  • ajax请求后台得到json数据后动态生成树形下拉框的方法

    如下所示: <select id="cc" class="easyui-combotree" style="width:580px;" name="rempId" data-options="required:true"></select> <script> $(function(){ $.ajax({ url:"departmentAction_getAllD

  • JS+CSS简单树形菜单实现方法

    本文实例讲述了JS+CSS简单树形菜单实现方法.分享给大家供大家参考.具体如下: 这是一款不错的CSS树形菜单,树状列表,当然不全是CSS实现,部分功能还使用了JavaScript代码进行配合,删减了修饰用的菜单图片,大家用的时候自己图片加进去,IMG标签大部分都预留了出来,稍懂Html语法的朋友很容易就看懂的. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-css-simple-tree-menu-style-codes/ 具体代码如下

  • php从数据库查询结果生成树形列表的方法

    本文实例讲述了php从数据库查询结果生成树形列表的方法.分享给大家供大家参考.具体分析如下: 本代码可以从数据库读取数据生成一个类似于windows的资源管理器的树形列表 <?php /* Here are the database definitions (for Solid) that i use in this code. * It should not be hard to adapt it to another database. */ /* CREATE TABLE dirent_t

随机推荐