當前位置:軟件學堂 > 資訊首頁 > 網絡編程 > 編程其他 > JS代碼實現樹形目錄菜單

JS代碼實現樹形目錄菜單

2012/11/2 20:23:01作者:佚名來源:網絡

移動端

【實例名稱】

JS代碼實現樹形目錄菜單

【實例描述】

網絡菜單有很多種形式,如彈出式、下拉式等。本例介紹樹型菜單的制作方法。

【實例代碼】

<html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>標題頁-學無憂(www.yzddtk.cn)</title> <script language="JavaScript"> //判斷瀏覽器的變量 NS4 = (document.layers) ? 1 : 0; IE4 = (document.all) ? 1 : 0; ver4 = (NS4 || IE4) ? 1 : 0; //定義各個層的位置及顯示狀態 if (ver4) {     with (document) {         write("<STYLE TYPE='text/css'>");         if (NS4) {             write(".parent {position:absolute; visibility:visible}");             write(".child {position:absolute; visibility:visible}");             write(".regular {position:absolute; visibility:visible}")         }         else {             write(".child {display:none}")         }         write("</STYLE>");     } } //當菜單打開時,頁面上菜單以下的東西的位置順序往下推,菜單合起時,菜單以下的東西自動上移。 function arrange() {     nextY = document.layers[firstInd].pageY + document.layers[firstInd].document.height;     for (i=firstInd+1; i<document.layers.length; i++) {         whichele = document.layers[i];         if (whichele.visibility != "hide") {             whichele.pageY = nextY;             nextY += whichele.document.height;         }     } } //初始化菜單 function initIt(){     if (!ver4) return;     if (NS4) {         for (i=0; i<document.layers.length; i++) {             whichele = document.layers[i];             if (whichele.id.indexOf("Child") != -1) whichele.visibility = "hide";        }         arrange();     }     else {         divColl = document.all.tags("DIV");         for (i=0; i<divColl.length; i++) {             whichele = divColl(i);             if (whichele.className == "child") whichele.style.display = "none";         }     } } //展開菜單的方法 function expandIt(ele) {     if (!ver4) return;     if (IE4) {         whichele = eval(ele + "Child");         if (whichele.style.display == "none") {             whichele.style.display = "block";         }         else {             whichele.style.display = "none";         }     }     else {         whichele = eval("document." + ele + "Child");         if (whichele.visibility == "hide") {             whichele.visibility = "show";         }         else {             whichele.visibility = "hide";         }         arrange();     } } onload = initIt; </script> </head> <body>       <div id="menuParent" class="parent">     <a href="#" onClick="expandIt('menu'); return false" >父菜單一</a></div>       <div id="menuChild" class="child">      <a href="#" target="_blank" >子菜單一</a><br>  <a href="#" target="_blank" >子菜單二</a><br>  <a href="#" target="_blank" >子菜單三</a></div>       <div id="Menu2Parent" class="parent">     <a href="#" onClick="expandIt('Menu2'); return false" >父菜單二</a></div>       <div id="Menu2Child" class="child">      <a href="#" target="_blank" >子菜單一</a><br>      <a href="#" target="_blank" >子菜單二</a><br>      <a href="#" target="_blank" >子菜單三</a></div>       <div id="Menu3Parent" class="parent">  <a href="#" onClick="expandIt('Menu3'); return false" >父菜單三</a></div>       <div id="Menu3Child" class="child">   <a href="#" target="_blank" >子菜單一</a><br> <a href="#" target="_blank" >子菜單二</a><br> <a href="#" target="_blank" >子菜單三</a></div> </body> </html>

【運行效果】

 樹形目錄菜單運行效果

【難點剖析】

本例中因為沒有使用圖標,所以顯示效果并不是很理想,在實際應用中,可以在節點前使用文件夾圖標。本例的重點是對象的“visibility”屬性,其值為“show”時顯示子節點,為“hide”時隱藏節點。

【源碼下載】

為了JS代碼的準確性,請點擊:樹形目錄菜單 進行本實例源碼下載 

標簽: 菜單    

欧美一级一极性活片免费观看,欧美一级艳片欧美精品,欧美一级性爱大片,欧美一级囗交视频 视频,欧美一级特黄录像视频