这是个用css和javascript做的树,在IE6和Netscape7下做过测试。
分二个页面。tree.htm和subtree.htm
下面是代码:
tree.htm:
<!--
树:以下为web下的树实现。原理:根据innerHTML属性从subtree.htm中取的子结点的数据,
子结点数据取过来后保存到child属性下。
结点表示法:NodeX_X 第一个X表示第一层的序号为X,第二个X表示第二层序号为X。
NodeX_:为子结点
NodeX_Img:为结点的展开图标
NodeX_Txt:为结点的文本
存在的问题:在IE下结点之间存有一端距离。
使用:在substree.htm中写从数据源中取的数据。
点击结点要打开的窗口在selectNode(nodeId,url)中的url。
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<style type="text/css">
<!--
.tree {
width: 200px;
margin-top:0px;
}
.tree ul{
margin-left:0px;
text-align:left;
}
.tree ul ul{
padding-left:10px;
}
.tree li{
padding:0px 0 0 0;
border:0px 0 0 0;
list-style-type: none;
width: 10em;
}
.tree a {
padding: 0px 0;
text-decoration: none;
color: #000;
}
.tree a:hover {
color: #faf5f8;
background: #879186;
}
.tree span{
padding: 0px 0;
}
.selected{
background: #879186;
}
.unselected{
background:#FFFFFF;
}
-->
</style>
</head>
<script language="javascript" type="text/JavaScript">
<!--
function expandNode(parentNode,id,url){
var node = document.getElementById(parentNode);
if(node != null){
var nodeChild = document.getElementById(parentNode+"_");
var nodeImg = document.getElementById(parentNode+"_Img");
if(node.expand!="yes"){
if(node.loaded !="yes"){
hiddenframe.location.replace("subtree.htm");
node.loaded="yes";
nodeChild.child="";
}
if(nodeChild != null){
if(nodeChild.child !="");
nodeChild.innerHTML=nodeChild.child;
//nodeChild.style.visibility="visible";
// nodeChild.style.height="auto";
}
if(nodeImg != null){
nodeImg.src ="image/tree_minus.gif";
}
node.expand ="yes";
}else{
var selectId = document.form1.selectId.value;
if(selectId.indexOf(parentNode)>-1){
selectNode(parentNode,"");
}
//nodeChild.style.visibility="hidden";
// nodeChild.style.height="0px";
nodeChild.child=nodeChild.innerHTML;
nodeChild.innerHTML="";
if(nodeImg !=null){
nodeImg.src ="image/tree_plus.gif";
}
node.expand="no";
}
//alert("#"+nodeChild.innerHTML+"#");
}
}
function selectNode(nodeId,url){
var node = document.getElementById(nodeId+"_Txt");
if(node != null){
var selectNode =document.getElementById(document.form1.selectId.value+"_Txt");
if(selectNode != null){
selectNode.className="unselected";
}
node.className="selected";
document.form1.selectId.value=nodeId;
}
}
-->
</script>
<body>
<div class="tree">
<ul>
<li id="Node0"><a
href="javascript:expandNode('Node0',0,'')"><img id ="Node0_Img"
src="image/tree_plus.gif" border="0" /></a><span
id="Node0_Txt"><a
href="javascript:selectNode('Node0','url')">系统</a></span>
</li>
<ul id="Node0_"></ul>
</ul>
</div>
<iframe width="100%" height="0" name="hiddenframe"></iframe>
<form name="form1">
<input name="selectId" type="hidden" value="dd">
</form>
</body>
</html>
subtree.htm:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<script language="javascript" type="text/JavaScript">
<!--
var x =parent.document.getElementById("Node0_");
if(x != null){
x.innerHTML="<li
id=\"Node0_0\"><a
href=\"javascript:expandNode('Node0_0',1,'')\"><img
id=\"Node0_0_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_0_Txt\"><a
href=\"javascript:selectNode('Node0_0','')\">应用系统</a><
/span></li><ul id=\"Node0_0_\"></ul><li
id=\"Node0_1\"><a
href=\"javascript:expandNode('Node0_1',2,'')\"><img
id=\"Node0_1_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_1_Txt\"><a
href=\"javascript:selectNode('Node0_1','')\">基础信息</a><
/span></li><ul id=\"Node0_1_\"></ul><li
id=\"Node0_2\"><a
href=\"javascript:expandNode('Node0_2',3,'')\"><img
id=\"Node0_2_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_2_Txt\"><a
href=\"javascript:selectNode('Node0_2','')\">计划管理</a><
/span></li><ul id=\"Node0_2_\"></ul><li
id=\"Node0_3\"><a
href=\"javascript:expandNode('Node0_3',4,'')\"><img
id=\"Node0_3_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_3_Txt\"><a
href=\"javascript:selectNode('Node0_3','')\">设备维护</a><
/span></li><ul id=\"Node0_3_\"></ul><li
id=\"Node0_4\"><a
href=\"javascript:expandNode('Node0_4',5.'')\"><img
id=\"Node0_4_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_4_Txt\"><a
href=\"javascript:selectNode('Node0_4','')\">变电运行</a><
/span></li><ul id=\"Node0_4_\"></ul><li
id=\"Node0_5\"><a
href=\"javascript:expandNode('Node0_5',6,'')\"><img
id=\"Node0_5_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_5_Txt\"><a
href=\"javascript:selectNode('Node0_5','')\">物资</a><
/span></li><ul id=\"Node0_5_\"></ul><li
id=\"Node0_6\"><a
href=\"javascript:expandNode('Node0_6',7,'')\"><img
id=\"Node0_6_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_6_Txt\"><a
href=\"javascript:selectNode('Node0_6','')\">工程</a><
/span></li><ul id=\"Node0_6_\"></ul><li
id=\"Node0_7\"><a
href=\"javascript:expandNode('Node0_7',8,'')\"><img
id=\"Node0_7_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_7_Txt\"><a
href=\"javascript:selectNode('Node0_7','')\">调度</a><
/span></li><ul id=\"Node0_7_\"></ul><li
id=\"Node0_8\"><a
href=\"javascript:expandNode('Node0_8',9.'')\"><img
id=\"Node0_8_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_8_Txt\"><a
href=\"javascript:selectNode('Node0_8','')\">安监</a><
/span></li><ul id=\"Node0_8_\"></ul><li
id=\"Node0_9\"><a
href=\"javascript:expandNode('Node0_9',10,'')\"><img
id=\"Node0_9_Img\" src=\"image/tree_plus.gif\" border=\"0\"
/></a><span id=\"Node0_9_Txt\"><a
href=\"javascript:selectNode('Node0_9','')\">输配电运行</a><
/span></li><ul id=\"Node0_9_\"></ul>";
}
-->
</script>
</body>
</html>