麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

javascript動(dòng)態(tài)生成樹(shù)形菜單的方法

2024-05-06 16:25:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了javascript動(dòng)態(tài)生成樹(shù)形菜單的方法,涉及JavaScript針對(duì)頁(yè)面元素與屬性的動(dòng)態(tài)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了javascript動(dòng)態(tài)生成樹(shù)形菜單的方法。分享給大家供大家參考,具體如下:

 

 
  1. //======================================== 
  2. //Envrionment to hold Listeners 
  3. //======================================== 
  4. tv_listeners = new Array() ; 
  5. function listener( type , handler ) { 
  6. this.type = type ; 
  7. this.handler = handler ; 
  8. this.id = tv_listeners.length ; 
  9. tv_listeners[ tv_listeners.length ] = this ; 
  10. function addListener( type , handler ) { 
  11. new listener( type , handler ) ;  
  12. //=== END ===== 
  13. //========================================= 
  14. // Hold the top item 
  15. //========================================= 
  16. tv_topnodeitem = null ; 
  17. //===== END ======= 
  18. //========================================= 
  19. //Hold nodeitems , and supply a nodeitem Register 
  20. //========================================= 
  21. nodeitems = new Array() ; 
  22. function nodeitemRegister( obj ) { 
  23. nodeitems[ nodeitems.length ] = obj ; 
  24. return nodeitems.length - 1 ; 
  25. //=== END ======= 
  26. //================================= 
  27. //Custom a stack 
  28. //Class : stack 
  29. //metheds : get() 
  30. // put( obj ) 
  31. //================================= 
  32. function stack() { 
  33. this.value = new Array() ; 
  34. this.cursor = 0 ; 
  35. function stack_get() { 
  36. this.cursor = this.cursor - 1 ; 
  37. return this.value[ this.cursor ] ; 
  38. function stack_put( obj ) { 
  39. this.value[ this.cursor ] = obj ; 
  40. this.cursor = this.cursor + 1 ; 
  41. stack.prototype.get = stack_get ; 
  42. stack.prototype.put = stack_put ; 
  43. //=======END ========== 
  44. //========================================= 
  45. // Define a public stack 
  46. //========================================= 
  47. userstack = new stack() ; 
  48. //====== END =========== 
  49. //========================================= 
  50. //Image List 
  51. //========================================= 
  52. treeview_box_0_none = "../images/4_clos.gif" ; 
  53. treeview_box_0_line = "../images/4_none.gif" ; 
  54. treeview_box_2_open = "../images/2_open.gif" ; 
  55. treeview_box_2_none = "../images/2_none.gif" ; 
  56. treeview_box_2_close = "../images/2_clos.gif" ; 
  57. treeview_box_1_open = "../images/3_open.gif" ; 
  58. treeview_box_1_none = "../images/3_none.gif" ; 
  59. treeview_box_1_close = "../images/3_clos.gif" ; 
  60. //=============================================== 
  61. //Class : nodeitem 
  62. //status------------------------1:two-direction 0:nobox 0: disactivite 
  63. // 2:three-0direction 1:close-box 1: activite 
  64. // 2:open-box 
  65. //=============================================== 
  66. function nodeitem( parentkey , key , lable , img ) { 
  67. this.lable = lable ; 
  68. this.key = key ; 
  69. this.parent = findNode( parentkey ) ; 
  70. ifthis.parent != null ) { 
  71. aa = this.parent.status ; 
  72. if( aa.substring( 1 , 2 ) == "0" ) 
  73. this.parent.status = aa.substring( 0 , 1 ) + "1" + aa.substring( 2 , 3 ) ; 
  74. ifthis.parent.maxsubitem != null )  
  75. this.parent.maxsubitem.status = "2" + this.parent.maxsubitem.status.substring( 1 , 3 ) ; 
  76. this.parent.subitems[ this.parent.subitems.length ] = this ; 
  77. this.parent.maxsubitem = this ;  
  78. else { 
  79. if( tv_topnodeitem != null ) { 
  80. alert( "不能有兩個(gè)頂項(xiàng)!" ) ; 
  81. return ; 
  82. tv_topnodeitem = this ; 
  83. this.img = img ; 
  84. this.tag = null ; 
  85. this.status = "100" ; 
  86. this.subitems = new Array() ; 
  87. this.maxsubitem = null ; 
  88. this.id = nodeitemRegister( this ) ; 
  89. //********************** 
  90. this.questionId = 0; 
  91. this.description = ""
  92. //this.url = null; 
  93. //********************** 
  94. //added by msb for the sort and move up/down 
  95. /*if ( this == tv_topnodeitem ) 
  96. this.nodeIndex = 0; 
  97. else { 
  98. this.nodeIndex = this.parent.subitems.length; 
  99. }*/ 
  100. //end added 
  101. //added by msb for the sort and move up/down 
  102. function nodeitem_moveUp() { 
  103. if (this == tv_topnodeitem) return//topitem 
  104. ssubitems = this.parent.subitems; 
  105. for ( i=0; i<ssubitems.length; i++ ) { 
  106. if( ssubitems[i] == this ) { 
  107. break
  108. if (i==0) return
  109. ssubitems[i] = ssubitems[i-1]; 
  110. ssubitems[i-1] = this
  111. if (i==ssubitems.length-1) { 
  112. ssubitems[i-1].status = "2" + ssubitems[i-1].status.substring(1, 3); 
  113. ssubitems[i].status = "1" + ssubitems[i].status.substring(1, 3); 
  114. /* 
  115. itemTemp = this; 
  116. ssubitems[this.nodeIndex-1] */ 
  117. /* for ( i=0; i<ssubitems.length; i++ ) { 
  118. if( ssubitems[i] != null && ssubitems[i].nodeIndex == (this.nodeIndex-1) ) 
  119. previousitem = ssubitems[i] 
  120. } 
  121. previousitem.nodeIndex = this.nodeIndex; 
  122. this.nodeIndex = this.nodeIndex -1; 
  123. swap(this,previousitem); 
  124. */ 
  125. //label_on_click(this.id); 
  126. this.parent.refresh(); 
  127. lable_on_click(this.id); 
  128. }//moveUp() 
  129. function nodeitem_moveDown() { 
  130. if (this == tv_topnodeitem) return//topitem 
  131. ssubitems = this.parent.subitems; 
  132. for ( i=0; i<ssubitems.length; i++ ) { 
  133. if( ssubitems[i] == this ) { 
  134. break
  135. if (i==ssubitems.length-1) return
  136. ssubitems[i] = ssubitems[i+1]; 
  137. ssubitems[i+1] = this
  138. if (i==ssubitems.length-2) { 
  139. ssubitems[i+1].status = "1" + ssubitems[i+1].status.substring(1, 3); 
  140. ssubitems[i].status = "2" + ssubitems[i].status.substring(1, 3); 
  141. this.parent.refresh(); 
  142. lable_on_click(this.id); 
  143. }//moveDown() 
  144. /*function swap (item1, item2) { 
  145. nodeitems[item1.id] = item2; 
  146. nodeitems[item2.id] = item1; 
  147. idTemp = item1.id; 
  148. item1.id = item2.id; 
  149. item2.id = idTemp; 
  150. }*/ 
  151. //end added 
  152. function nodeitem_setTag( obj ) { 
  153. this.tag = obj ; 
  154. function nodeitem_getTag() { 
  155. return this.tag ; 
  156. function nodeitem_show() { 
  157. str = "<span id = 'preface" + this.id + "'><table border='0' cellspacing='0' cellpadding='0'><tr><td>" ; 
  158. str_f = "" ; 
  159. for( j = this.parent ; j != null ; j = j.parent ) { 
  160. if( j.status.substring( 0 , 1 ) == 1 ) 
  161. str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ; 
  162. else 
  163. str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ; 
  164. str = str + str_f ; 
  165. str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ; 
  166. switchthis.status.substring( 0 , 2 ) ) { 
  167. case "10" : str += treeview_box_1_none ; break ; 
  168. case "11" : str += treeview_box_1_close ; break ; 
  169. case "12" : str += treeview_box_1_open ; break ; 
  170. case "20" : str += treeview_box_2_none ; break ; 
  171. case "21" : str += treeview_box_2_close ; break ; 
  172. case "22" : str += treeview_box_2_open ; break ; 
  173. str += "' align='absmiddle' onclick='box_on_click(this)'>" ; 
  174. ifthis.img == "" ) 
  175. str += this.img ; 
  176. else 
  177. str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ; 
  178. 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>" ;  
  179. str += "</span><span id = 'tv_panel_" + this.id + "' style='display:" ; 
  180. ifthis.status.substring( 1 , 2 ) == '2' ) 
  181. str += "" ; 
  182. else 
  183. str += "none" ; 
  184. str += "'></span>" ; 
  185. ifthis.parent == null )  
  186. for(var i in document.all){ 
  187. if (document.all[i].tagName == "BODY"
  188. document.all[i].insertAdjacentHTML( "AfterBegin" , str ) ; 
  189. break 
  190. else 
  191. document.all( "tv_panel_" + this.parent.id ).insertAdjacentHTML( "BeforeEnd" , str ) ; 
  192. for( m = 0 ; m < this.subitems.length ; m ++ )  
  193. ifthis.subitems[ m ] != null ) { 
  194. userstack.put( m ) ; 
  195. this.subitems[ m ].show() ; 
  196. m = userstack.get() ; 
  197. function nodeitem_refresh() { 
  198. str = "<table border='0' cellspacing='0' cellpadding='0'><tr><td>" ; 
  199. str_f = "" ; 
  200. for( j = this.parent ; j != null ; j = j.parent ) { 
  201. if( j.status.substring( 0 , 1 ) == 1 ) 
  202. str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ; 
  203. else 
  204. str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ; 
  205. str = str + str_f ; 
  206. str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ; 
  207. switchthis.status.substring( 0 , 2 ) ) { 
  208. case "10" : str += treeview_box_1_none ; break ; 
  209. case "11" : str += treeview_box_1_close ; break ; 
  210. case "12" : str += treeview_box_1_open ; break ; 
  211. case "20" : str += treeview_box_2_none ; break ; 
  212. case "21" : str += treeview_box_2_close ; break ; 
  213. case "22" : str += treeview_box_2_open ; break ; 
  214. str += "' align='absmiddle' onclick='box_on_click(this)'>" ; 
  215. ifthis.img == "" ) 
  216. str += this.img ; 
  217. else 
  218. str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ; 
  219. 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>" ;  
  220. document.all( "preface" + this.id ).innerHTML = str ; 
  221. document.all( "tv_panel_" + this.id ).innerHTML = "" ; 
  222. for( m = 0 ; m < this.subitems.length ; m ++ ) 
  223. ifthis.subitems[ m ] != null ) { 
  224. userstack.put( m ) ; 
  225. this.subitems[ m ].show() ; 
  226. m = userstack.get() ; 
  227. function nodeitem_remove() { 
  228. pparent = this.parent ; 
  229. if( pparent == null ) { 
  230. removenodeitem( this.id ) ; 
  231. for(var i in document.all){ 
  232. if (document.all[i].tagName == "BODY"
  233. document.all[i].innerHTML = "" ; 
  234. break 
  235. return ; 
  236. lastsubitem = null ; 
  237. for( i = 0 ; i < pparent.subitems.length ; i ++ ) 
  238. if( pparent.subitems[ i ] != null )  
  239. if ( pparent.subitems[ i ] == this )  
  240. pparent.subitems[ i ] = null ; 
  241. else 
  242. lastsubitem = pparent.subitems[ i ] ; 
  243. pparent.maxsubitem = lastsubitem ;  
  244. if( lastsubitem == null )  
  245. pparent.status = pparent.status.substring( 0 , 1 ) + "0" + pparent.status.substring( 2 , 3 ) ; 
  246. else 
  247. pparent.maxsubitem.status = "1" + pparent.maxsubitem.status.substring( 1 , 3 ) ; 
  248. removenodeitem( this.id ) ; 
  249. //added by msb for move up/down 
  250. arrTemp = new Array(); 
  251. j = 0; 
  252. for ( i=0; i<pparent.subitems.length; i++ ) { 
  253. if ( pparent.subitems[i] != null ) { 
  254. arrTemp[j] = pparent.subitems[i]; 
  255. j++; 
  256. this.parent.subitems = arrTemp; 
  257. //end added 
  258. pparent.refresh() ; 
  259. //tv_topnodeitem.refresh() ; 
  260. function removenodeitem( id ) { 
  261. curitem = nodeitems[ id ] ; 
  262. nodeitems[ id ] = null ; 
  263. for( m = 0 ; m < curitem.subitems.length ; m ++ )  
  264. if( curitem.subitems[ m ] != null ) { 
  265. userstack.put( m ) ; 
  266. removenodeitem( curitem.subitems[ m ].id ) ; 
  267. m = userstack.get() ; 
  268. function nodeitem_boxclick() { 
  269. ifthis.status.substring( 1 ,2 ) == "0" ) 
  270. return ;  
  271. ifthis.status.substring( 1 ,2 ) == "1" )  
  272. this.open() ; 
  273. else 
  274. this.close() ; 
  275. function nodeitem_close() { 
  276. this.status = this.status.substring( 0 , 1 ) + "1" + this.status.substring( 2 , 3 ) ; 
  277. document.all( "tv_panel_" + this.id ).style.display = "none" ; 
  278. eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_close" ) ; 
  279. function nodeitem_open() { 
  280. this.status = this.status.substring( 0 , 1 ) + "2" + this.status.substring( 2 , 3 ) ; 
  281. document.all( "tv_panel_" + this.id ).style.display = "" ; 
  282. eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_open" ) ; 
  283. //added by msb for the move up/down 
  284. nodeitem.prototype.moveUp = nodeitem_moveUp; 
  285. nodeitem.prototype.moveDown = nodeitem_moveDown; 
  286. //end added 
  287. nodeitem.prototype.show = nodeitem_show ; 
  288. nodeitem.prototype.refresh = nodeitem_refresh ; 
  289. nodeitem.prototype.boxclick = nodeitem_boxclick ; 
  290. nodeitem.prototype.close = nodeitem_close ; 
  291. nodeitem.prototype.open = nodeitem_open ; 
  292. nodeitem.prototype.remove = nodeitem_remove ; 
  293. nodeitem.prototype.setTag = nodeitem_setTag ; 
  294. nodeitem.prototype.getTag = nodeitem_getTag ; 
  295. //========================================================== 
  296. // Public Methods 
  297. //========================================================== 
  298. function showTV() { 
  299. tv_topnodeitem.show() ; 
  300. function findNode( key ) { 
  301. pppp = null
  302. for( i = 0 ; i < nodeitems.length ; i ++ ) { 
  303. if( nodeitems[ i ] != null ) { 
  304. if( nodeitems[ i ].key == key ) { 
  305. pppp = nodeitems[ i ] ; 
  306. return pppp ; 
  307. function addNode( parentkey , key , lable , img ) { 
  308. return new nodeitem( parentkey , key , lable , img ) ; 
  309. function deleteNode( key ) { 
  310. curNode = findNode( key ) ; 
  311. if( curNode == null ) 
  312. return false ; 
  313. curNode.remove() ;  
  314. return true ; 
  315. //====== END ================ 
  316. //=========================================================== 
  317. // Events 
  318. //=========================================================== 
  319. function box_on_click( obj ) { 
  320. nodeitems[ obj.nodeid ].boxclick() ; 
  321. tv_curlable = null ; 
  322. tv_curlable_f = null ; 
  323. function lable_on_click( id ) { 
  324. key = nodeitems[ id ].key ; 
  325. if( nodeitems[ id ].parent == null ) 
  326. parentkey = "" ; 
  327. else 
  328. parentkey = nodeitems[ id ].parent.key ; 
  329. if( tv_curlable != null ) { 
  330. tv_curlable.bgColor = "transparent" ; 
  331. tv_curlable.style.color = "#333333" ; 
  332. tv_curlable_f.bgColor = "transparent" ; 
  333. tv_curlable = document.all("lablePanel"+id) ; 
  334. tv_curlable.bgColor = "#000000" ; 
  335. tv_curlable.style.color = "#FFFFFF" ; 
  336. tv_curlable_f = document.all("f_lablePanel"+id) ; 
  337. tv_curlable_f.bgColor = "#888888" ; 
  338. for( i = 0 ; i < tv_listeners.length ; i ++ )  
  339. if( tv_listeners[ i ].type == "click" ) { 
  340. h = tv_listeners[ i ].handler ; 
  341. eval( h + "( '" + key + "' , '" + parentkey + "' ) ; " ) ;  
  342. function lable_on_dblclick( id ) { 
  343. key = nodeitems[ id ].key ; 
  344. if( nodeitems[ id ].parent == null ) 
  345. parentkey = "" ; 
  346. else 
  347. parentkey = nodeitems[ id ].parent.key ; 
  348. if( tv_curlable != null ) { 
  349. tv_curlable.bgColor = "transparent" ; 
  350. tv_curlable.style.color = "#333333" ; 
  351. tv_curlable_f.bgColor = "transparent" ; 
  352. tv_curlable = document.all("lablePanel"+id) ; 
  353. tv_curlable.bgColor = "#000000" ; 
  354. tv_curlable.style.color = "#FFFFFF" ; 
  355. tv_curlable_f = document.all("f_lablePanel"+id) ; 
  356. tv_curlable_f.bgColor = "#888888" ; 
  357. for( i = 0 ; i < tv_listeners.length ; i ++ )  
  358. if( tv_listeners[ i ].type == "dblclick" ) { 
  359. h = tv_listeners[ i ].handler ; 
  360. eval( h + "( '" + key + "' , '" + parentkey + "' ) ; " ) ;  

javascript調(diào)用

 

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

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 国产一精品久久99无吗一高潮 | 黄色美女免费 | 亚洲成人在线视频网站 | 男男啪羞羞视频网站 | 毛片a级毛片免费播放100 | 欧美性猛交一区二区三区精品 | 亚洲一区二区三区视频免费 | 手机av免费电影 | 在线免费亚洲 | 伊人网站| 亚洲成人精品久久 | 午夜色片| 欧美激情第一区 | 国产91精品亚洲精品日韩已满 | h视频在线免费观看 | 狠狠操天天射 | 久草视频福利在线观看 | www.17c亚洲蜜桃 | 欧美精品欧美极品欧美激情 | 一区二区国产在线 | 免费观看一级欧美大 | 日韩视频在线观看免费 | 激情小说激情图片激情电影 | www国产网站 | 成年人网站国产 | 男女视频免费看 | 香蕉国产片| 欧美a级在线免费观看 | 久久午夜神器 | 欧美成人综合视频 | 午夜激情视频网站 | 精品中文一区 | 久草成人在线观看 | av日韩一区二区三区 | 欧美日韩色 | 国产精品999在线观看 | 欧美日韩在线播放 | 成片免费大全 | 久久91精品国产91久久yfo | 成人国产综合 | 中文字幕电影免费播放 |