準備工作:
請從指令碼之家http://www.jb51.net/jiaoben/31974.html下載dtree.zip檔案
dtree.zip壓縮包介紹:
dtree是一個由JavaScript編寫成的簡單的樹形菜單組件,目前免費並且開源。
目前有很多的樹形菜單組件(比如ext),dtree是一種簡單易懂的js組件,
不需要複雜的操作即可生產,同時支援動態從資料庫引入資料
解壓後有以下幾部分:
img檔案夾: 包含樹形菜單顯示需要的表徵圖
api.html : 作者寫的dtree協助文檔
dtree.css: 樹形菜單的樣式
dtree.js : js核心檔案,代碼都在其中
example01.html:樹形菜單一實例 dtree主要方法介紹:
dtree主要方法介紹:
add(parameters):添加一個樹節點,實際參數有9個add(id,pid,name,url,title,target,icon,iconOpen,open);
位置 參數別名 類型 功能
1 id int 節點自身的id(唯一)
2 pid int 節點的父節點id
3 name string 節點顯示在頁面上的名稱
4 url string 節點的連結地址
5 title string 滑鼠放在節點上顯示的提示資訊
6 target string 節點連結所開啟的目標frame
7 icon string 節點關閉狀態時顯示的表徵圖
8 iconOpen string 節點開啟狀態時顯示的表徵圖
9 open bool 節點第一次載入是否開啟
註:dtree.js檔案中是一些預設圖片的路徑,可以自己配置圖片和路徑,我下載的在44~57行
openAll()開啟全部節點,可在樹對象建立前或建立後調用
closeAll()關閉全部節點,可在樹對象建立前或建立後調用
openTo(id,select)開啟指定id的節點,可以傳兩個參數:
id 指定需要開啟的節點的唯一id
select 是否讓該節點處於選中狀態
config配置
變數 類型 預設值 描述
target string 所有節點的target
folderLinks bool true 檔案夾可被連結
useSelection bool true 節點可被選擇高亮
useCookies bool true 樹可以使用cookie記住狀態
useLines bool true 建立帶結構連接線的樹
useIcons bool true 建立帶有圖表的樹
useStatusText bool false 用節點名替代顯示在狀態列的節點url
closeSameLevel bool false 同級節點只允許一個節點處於開啟狀態
inOrder bool false 加速父節點樹的顯示
例如:tree.config.closeSameLevel=true;表示開啟某級節點時,該級其他處於開啟狀態的同級節點會被關閉
範例程式碼:
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Tree</title>
<link rel="StyleSheet" href="dtree.css" type="text/css" /><!-- 引入css樣式表 -->
<script type="text/javascript" src="dtree.js"></script><!-- 引入js指令碼 -->
</head>
<body>
<div class="dtree"><!--建立一個div層,指定class為“dtree”,此時該層就引用了dtree的樣式 -->
<script type="text/javascript">
d = new dTree('d');//new一個樹對象
//設定樹的節點及其相關屬性
d.add(0,-1,'My example tree');
d.add(1,0,'Node 1','example01.html');
d.add(2,0,'Node 2','example01.html');
d.add(3,1,'Node 1.1','example01.html');
d.add(4,0,'Node 3','example01.html');
d.add(5,3,'Node 1.1.1','example01.html');
d.add(6,5,'Node 1.1.1.1','example01.html');
d.add(7,0,'Node 4','example01.html');
d.add(8,1,'Node 1.2','example01.html');
d.add(9,0,'My Pictures','example01.html','Pictures I\'ve taken over the years','','','img/imgfolder.gif');
d.add(10,9,'The trip to Iceland','example01.html','Pictures of Gullfoss and Geysir');
d.add(11,9,'Mom\'s birthday','example01.html');
d.add(12,0,'Recycle Bin','example01.html','','','img/trash.gif');
//config配置,設定檔案夾不能被連結,即有子節點的不能被連結。
d.config.folderLinks=false;
document.write(d);
</script>
</div>
</body>
</html>