本文介绍了一种用javascript动态生成树形菜单的方法,供大家参考:


/ / ========================================
/ /环境抓住听众
/ / ========================================
tv_listeners =新的数组();
函数侦听器(类型、处理程序){
this.type =型;
this.handler =处理程序;
tv_listeners.length this.id =;
tv_listeners { } =这长度tv_listeners;
}
功能听听(类型、Handler){
新侦听器(类型、处理程序);
}
结尾闪烁
/ / =========================================
顶部项目
/ / =========================================
tv_topnodeitem = null;
/ / =====端=======
/ / =========================================
/ /保持nodeitems,并提供一个nodeitem登记
/ / =========================================
nodeitems =新的数组();
功能nodeitemregister(obj){
nodeitems { nodeitems长度} =目标;
返回nodeitems.length - 1;
}
/ / = = =结束=======
/ / =================================
自定义堆栈
类:堆栈
/ /方法:把()
/ /把(obj)
/ / =================================
函数堆栈(){
this.value =新的数组();
this.cursor = 0;
}
功能stack_get(){
this.cursor = this.cursor - 1;
返回这个值{这个};
}
功能stack_put(obj){
这个值{光标} =目标;
this.cursor = this.cursor + 1;
}
stack.prototype.get = stack_get;
stack.prototype.put = stack_put;
/ / =======端==========
/ / =========================================
公共堆栈定义
/ / =========================================
值=新的堆栈();
/ / ======端===========
/ / =========================================
图像列表
/ / =========================================
treeview_box_0_none = 。 / / 4_clos GIF图像。;
treeview_box_0_line = 。 / / 4_none GIF图像。;
treeview_box_2_open = 。 / / 2_open GIF图像。;
treeview_box_2_none = 。 / / 2_none GIF图像。;
treeview_box_2_close = 。 / / 2_clos GIF图像。;
treeview_box_1_open = 。 / / 3_open GIF图像。;
treeview_box_1_none = 。 / / 3_none GIF图像。;
treeview_box_1_close = 。 / / 3_clos GIF图像。;
/ / ===============================================
/ /类:nodeitem
/ /状态------------------------ 1:两方向0:0:disactivite NOBOX
1:关闭对话框1:活动 / / 2:three-0direction
2:打开框
/ / ===============================================
功能nodeitem(ParentKey,关键,标签,IMG){
this.lable =标签;
this.key =关键;
this.parent = findnode(ParentKey);
如果(this.parent!= NULL){
AA = this.parent.status;
如果(aa.substring(1, 2)= =0)
aa.substring this.parent.status =(0, 1)+1+ aa.substring(2, 3);
如果(this.parent.maxsubitem!= null)
this.parent.maxsubitem.status =2+ this.parent.maxsubitem.status.substring(1, 3);
这个父。子项{。父。子项。长度} =这;
这this.parent.maxsubitem =;
}
{其他
如果(tv_topnodeitem!= NULL){
警报()没有两个顶部项目!)
返回;
}
这tv_topnodeitem =;
}
this.img = IMG;
this.tag = null;
this.status =100;
this.subitems =新的数组();
this.maxsubitem = null;
nodeitemregister this.id =(本);
/ / **********************
this.questionid = 0;
this.description = ;
/ / this.url = null;
/ / **********************
/ /加MSB的排序和上升/下降
/ *如果(= tv_topnodeitem)
{
this.nodeindex = 0;
{人}
this.nodeindex = this.parent.subitems.length;
}
添加
}
/ /加MSB的排序和上升/下降
功能nodeitem_moveup(){
如果(= = tv_topnodeitem回报; / / topitem)
ssubitems = this.parent.subitems;
为(i = 0;i < ssubitems.length;i++){
如果(ssubitems {我} = =这){
打破;
}
}
如果(i = 0)返回;
我ssubitems { } = { } ssubitems I-1;
ssubitems {·} =这;
如果(我= = ssubitems。length-1){
ssubitems {·}。状态=2+ ssubitems {·}。status.substring(1, 3);
我ssubitems { }。状态=1+ ssubitems {我}。status.substring(1, 3);
}
*
这itemtemp =;
* / ssubitems {这nodeindex-1 }。
/ *(i = 0;i < ssubitems.length;i++){
如果(ssubitems {我}!=空= ssubitems {我}。nodeindex(这个。nodeindex-1))
previousitem = ssubitems {我}
}
previousitem.nodeindex = this.nodeindex;
this.nodeindex = this.nodeindex - 1;
互换(这previousitem);
* /
/ / label_on_click(这个ID);
This.parent.refresh();
lable_on_click(这个ID);
} / /上()
功能nodeitem_movedown(){
如果(= = tv_topnodeitem回报; / / topitem)
ssubitems = this.parent.subitems;
为(i = 0;i < ssubitems.length;i++){
如果(ssubitems {我} = =这){
打破;
}
}
如果(我= = ssubitems。length-1)返回;
我ssubitems { } = { 1 }我ssubitems;
ssubitems { 1 } =这我+;
如果(我= = ssubitems。长度为2){
ssubitems {我+ 1 }。状态=1+ ssubitems { 1 }我status.substring(1, 3);
我ssubitems { }。状态=2+ ssubitems {我}。status.substring(1, 3);
}
This.parent.refresh();
lable_on_click(这个ID);
} / /向下移动()
/ *功能互换(1,2){
nodeitems { } = 2 1。ID;
nodeitems { } = 1 2。ID;
idtemp = item1.id;
item1.id = item2.id;
item2.id = idtemp;
}
添加
功能nodeitem_settag(obj){
this.tag = obj;
}
功能nodeitem_gettag(){
返回this.tag;
}
功能nodeitem_show(){
;
str_f = ;
为(J = this.parent;J!= null;J = j.parent){
如果(j.status.substring(0, 1)= = 1)
str_f = + str_f;
其他的
str_f = + str_f;
}
STR =力量+ str_f;
str =javascript动态生成树形菜单的方法 onclick= 'box_on_click(这);
如果(this.img = = )
STR = this.img;
其他的
=;
this.lable STR + + + = ;
str =<跨度ID = 'tv_panel_ + this.id +风格=显示:;
如果(this.status.substring(1, 2)= = 2)
=;
其他的
否;
;
如果(this.parent = null)
对于(文档中的var i){
如果(文件。所有{我} tagname = =身体。)
{
文件。所有{我}。insertadjacenthtml(afterbegin
打破
}
}
其他的
document.all(tv_panel_+本。父母。ID)。InsertAdjacentHTML(beforeend
对(M = 0;M this.subitems.length;M + +)
如果(这项{米}!= NULL){
Userstack.put(M);
这个子项{男}显示();
M = userstack.get();
}
}
功能nodeitem_refresh(){
;
str_f = ;
为(J = this.parent;J!= null;J = j.parent){
如果(j.status.substring(0, 1)= = 1)
str_f = + str_f;
其他的
str_f = + str_f;
}
STR =力量+ str_f;
str =javascript动态生成树形菜单的方法 onclick= 'box_on_click(这);
如果(this.img = = )
STR = this.img;
其他的
=;
this.lable STR + + + = ;
document.all(前言+这个ID).innerHTML = str;
document.all(tv_panel_+这个ID).innerHTML = ;
对(M = 0;M this.subitems.length;M + +)
如果(这项{米}!= NULL){
Userstack.put(M);
这个子项{男}显示();
M = userstack.get();
}
}
功能nodeitem_remove(){
本this.parent =;
如果(本= = null){
Removenodeitem(这个ID);
对于(文档中的var i){
如果(文件。所有{我} tagname = =身体。)
{
文件。所有{我} .innerHTML = ;
打破
}
}
返回;
}
lastsubitem = null;
为(i = 0;i < pparent.subitems.length;i++)
如果(本项{我}!= null)
如果(本项{我= =这。)
本项{我} = null;
其他的
lastsubitem =本项{我};
pparent.maxsubitem = lastsubitem;
如果(lastsubitem = null)
pparent.status.substring pparent.status =(0, 1)+0+ pparent.status.substring(2, 3);
其他的
pparent.maxsubitem.status =1+ pparent.maxsubitem.status.substring(1, 3);
Removenodeitem(这个ID);
/ /添加的MSB上升/下降
arrtemp =新的数组();
j=0;
为(i = 0;i < pparent.subitems.length;i++){
如果(本项{我}!= NULL){
arrtemp { J } =本项{我};
++;
}
}
this.parent.subitems = arrtemp;
添加
Pparent.refresh();
/ / tv_topnodeitem.refresh();
}
功能removenodeitem(ID){
curitem = nodeitems {id};
nodeitems {id} = null;
对(M = 0;M curitem.subitems.length;M + +)
如果(curitem项{米}!= NULL){
Userstack.put(M);
Removenodeitem(curitem。子项{米}。ID);
M = userstack.get();
}
}
功能nodeitem_boxclick(){
如果(this.status.substring(1, 2)= =0)
返回;
如果(this.status.substring(1, 2)= =1)
This.open();
其他的
This.close();
}
功能nodeitem_close(){
this.status.substring this.status =(0, 1)+1+ this.status.substring(2, 3);
document.all(tv_panel_+这个ID)。style.display =没有;
eval(document.all('box +这个ID)。src= treeview_box_ this.status.substring+(0, 1)+_close );
}
功能nodeitem_open(){
this.status.substring this.status =(0, 1)+2+ this.status.substring(2, 3);
document.all(tv_panel_+这个ID)。style.display = ;
eval(document.all('box +这个ID)。src= treeview_box_ this.status.substring+(0, 1)+_open );
}
/ /加入MSB移动上/下
nodeitem.prototype.moveup = nodeitem_moveup;
nodeitem.prototype.movedown = nodeitem_movedown;
添加
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;
/ / ==========================================================
公共/ /方法
/ / ==========================================================
功能showtv(){
tv_topnodeitem.show();
}
功能findnode(关键){
PPPP = null;
为(i = 0;i < nodeitems.length;i++){
如果(nodeitems {我}!= NULL){
如果(nodeitems {我}。关键= =键){
PPPP = nodeitems {我};
}
}
}
返回PPPP;
}
功能ADDNODE(ParentKey,关键,标签,IMG){
返回新的nodeitem(ParentKey,关键,标签,IMG);
}
功能deletenode(关键){
curnode = findnode(关键);
如果(curnode = null)
返回false;
CurNode.remove();
返回true;
}
/ / ======端================
/ / ===========================================================
事件
/ / ===========================================================
功能box_on_click(obj){
nodeitems { obj。NodeID }。单击();
}
tv_curlable = null;
tv_curlable_f = null;
功能lable_on_click(ID){
关键= nodeitems {id}。关键;
如果(nodeitems {id}。母= = null)
ParentKey = ;
其他的
ParentKey = nodeitems {id}。parent.key;
如果(tv_curlable!= NULL){
tv_curlable.bgcolor =透明;
tv_curlable.style.color =# 333333 ;
tv_curlable_f.bgcolor =透明;
}
tv_curlable = document.all(lablepanel+身份证);
tv_curlable.bgcolor =# 000000 ;
tv_curlable.style.color =# ffffff ;
tv_curlable_f = document.all(f_lablepanel+身份证);
tv_curlable_f.bgcolor =# 888888 ;
为(i = 0;i < tv_listeners.length;i++)
如果(tv_listeners {我}。型= =点击){
H = tv_listeners {我},处理程序;
eval(H +(+ +键+ 、+ ParentKey +););
}
}
功能lable_on_dblclick(ID){
关键= nodeitems {id}。关键;
如果(nodeitems {id}。母= = null)
ParentKey = ;
其他的
ParentKey = nodeitems {id}。parent.key;
如果(tv_curlable!= NULL){
tv_curlable.bgcolor =透明;
tv_curlable.style.color =# 333333 ;
tv_curlable_f.bgcolor =透明;
}
tv_curlable = document.all(lablepanel+身份证);
tv_curlable.bgcolor =# 000000 ;
tv_curlable.style.color =# ffffff ;
tv_curlable_f = document.all(f_lablepanel+身份证);
tv_curlable_f.bgcolor =# 888888 ;
为(i = 0;i < tv_listeners.length;i++)
如果(tv_listeners {我}。型= =双击){
H = tv_listeners {我},处理程序;
eval(H +(+ +键+ 、+ ParentKey +););
}
}




Javascript调用



<!——
AddNode(-1,0,呵呵
AddNode(0,1,哈哈
AddNode(0,2,哈哈
AddNode(1,3,哈哈
AddNode(1,4,哈哈
AddNode(2,5,哈哈
AddNode(2,6,哈哈
ShowTV();
-->





希望本文能对javascript程序设计有所帮助。