关于我们 广告服务 社区论坛
设为首页 加入收藏

行业新闻
服 务 器
模版下载
建站指南
冲浪宝典
办公软件
网站运营
操作系统
QQ 专题
网页制作
安全防御
视频教程
网络编程
SEO专区
软件下载
图像设计
Cisco
网页特效
Wap 技术
联盟赚钱
网页素材
 首页 | 企业建站 | 网页制作 | 网站运营 | 网络编程 | 图像设计 | 冲浪宝典 | 操作系统 | SEO专区 | 联盟赚钱 | Cisco

欢迎来到e天下网络首页>>网页制作>>Javascript>>正文|XML+ JS创建树形菜单

XML+ JS创建树形菜单

[ 来路:21kn.com    时间:2007-7-7 20:04:34    点击: ]

 

简单说明:

思路:
从数据岛menuXML中读取数据,从树的根节点开始分析树,
利用 hasChildNodes() [方法:是否含有子节点 ] 判断当前
节点是否有子节点,如果有子节点继续向下分析 childNodes
[对象:子节点对象集合] ,否则返回当前分析结果(树结点对象)。

主要的函数:
createTree(thisn /*树结点*/ , sd/*树深度*/)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> DSTree </TITLE>
<META NAME="Author" CONTENT="sTarsjz@hotmail.com" >
<style>
body,td{font:12px verdana}
#treeBox{background-color:#fffffa;}
#treeBox .ec{margin:0 5 0 5;}
#treeBox .hasItems{font-weight:bold;height:20px;padding:3 6 0 6;margin:2px;cursor:hand;color:#555555;border:1px solid #fffffa;}
#treeBox .Items{height:20px;padding:3 6 0 6;margin:1px;cursor:hand;color:#555555;border:1px solid #fffffa;}
</style>
<base href="http://vip.5d.cn/star/dstree/" />
<script>
//code by star 20003-4-7
var HC = "color:#990000;border:1px solid #cccccc";
var SC = "background-color:#efefef;border:1px solid #cccccc;color:#000000;";
var IO = null;
function initTree(){
     var rootn = document.all.menuXML.documentElement;
     var sd = 0;
     document.onselectstart = function(){return false;}
     document.all.treeBox.appendChi ld(createTree(rootn,sd));
}
function createTree(thisn,sd){
     var nodeObj = document.createElement("span");
     var upobj = document.createElement("span");
     with(upobj){
           style.marginLeft = sd*10;
           className = thisn.hasChildNodes()?"hasItems":"Items";
           innerHTML = "<img src="http://www.21kn.com/Files/BeyondPic/2007-7/7/077720043418310.gif" class=ec>" + thisn.getAttribute("text") +"";
          
           onmousedown = function(){
                if(event.button != 1) return;
                if(this.getAttribute("cn")){
                      this.setAttribute("open",!this.getAttribute("open"));
                      this.cn.style.display = this.getAttribute("open")?"inline":"none";
                      this.all.tags("img")[0].src = this.getAttribute("open")?"expand.gif":"contract.gif";
                }
                if(IO){
                      IO.runtimeStyle.cssText = "";
                      IO.setAttribute("selected",false);
                }
                IO = this;
                this.setAttribute("selected",t rue);
                this.runtimeStyle.cssText = SC;
           }
           onmouseover = function(){
                if(this.getAttribute("selected "))return;
                this.runtimeStyle.cssText = HC;
           }
           onmouseout = function(){
                if(this.getAttribute("selected "))return;
                this.runtimeStyle.cssText = "";
           }
           oncontextmenu = contextMenuHandle;
           onclick = clickHandle;
     }

     if(thisn.getAttribute("treeId" ) != null){
           upobj.setAttribute("treeId",thisn.getAttribute("treeId"));
     }
     if(thisn.getAttribute("href") != null){
           upobj.setAttribute("href",thisn.getAttribute("href"));
     }
     if(thisn.getAttribute("target" ) != null){
           upobj.setAttribute("target",thisn.getAttribute("target"));
     }

     nodeObj.appendChild(upobj);
     nodeObj.insertAdjacentHTML("be foreEnd","<br>")

     if(thisn.hasChildNodes()){
           var i;
           var nodes = thisn.childNodes;
           var cn = document.createElement("span");
           upobj.setAttribute("cn",cn);
           if(thisn.getAttribute("open") != null){
                upobj.setAttribute("open",(thi sn.getAttribute("open")=="true"));
                upobj.getAttribute("cn").style .display = upobj.getAttribute("open")?"inline":"none";
                if( !upobj.getAttribute("open"))upobj.all.tags("img")[0].src ="contract.gif";
           }
          
           for(i=0;i<nodes.length;cn.appendChild(createTree(nodes [i++],sd+1)));
           nodeObj.appendChild(cn);
     }
     else{
           upobj.all.tags("img")[0].src ="endnode.gif";
     }
     return nodeObj;
}
window.onload = initTree;
</script>

<script>
function clickHandle(){
     // your code here
}
function contextMenuHandle(){
     event.returnValue = false;
     var treeId = this.getAttribute("treeId");
     // your code here
}
</script>
</HEAD>
<BODY>
<xml id=menuXML>
<?xml version="1.0" encoding="GB2312"?>
<DSTreeRoot text="根节点" open="true" href="http://" treeId="123">
     
     <DSTree text="技术论坛" open="false" treeId="">
           <DSTree text="5DMedia" open="false" href="http://" target="box" treeId="12">
                 <DSTree text="网页编码" href="http://" target="box" treeId="4353" />
                 <DSTree text="手绘" href="http://" target="box" treeId="543543" />
                 <DSTree text="灌水" href="http://" target="box" treeId="543543" />
           </DSTree>
           <DSTree text="BlueIdea" open="false" href="http://" target="box" treeId="213">
                 <DSTree text="DreamWeaver & JS" href="http://" target="box" treeId="4353" />
                 <DSTree text="FlashActionScript" href="http://" target="box" treeId="543543" />
           </DSTree>
           <DSTree text="CSDN" open="false" href="http://" target="box" treeId="432">
                 <DSTree text="JS" href="http://" target="box" treeId="4353" />
                 <DSTree text="XML" href="http://" target="box" treeId="543543" />
           </DSTree>
     </DSTree>

     <DSTree text="资源站点" open="false" treeId="">
           <DSTree text="素材屋" href="http://" target="box" treeId="12" />
           <DSTree text="桌面城市" open="false" href="http://" target="box" treeId="213">
                 <DSTree text="壁纸" href="http://" target="box" treeId="4353" />
                 <DSTree text="字体" href="http://" target="box" treeId="543543" />
           </DSTree>
           <DSTree text="MSDN" open="false" href="http://" target="box" treeId="432">
                 <DSTree text="DHTML" href="http://" target="box" treeId="4353" />
                 <DSTree text="HTC" href="http://" target="box" treeId="543543" />
                 <DSTree text="XML" href="" target="box" treeId="2312" />
           </DSTree>
     </DSTree>

</DSTreeRoot>
</xml>
<table style="position:absolute;left:100;top:100;">
<tr><td id=treeBox style="width:400px;height:200px;border:1px solid #cccccc;padding:5 3 3 5;" valign=top></td></tr>
<tr><td style="font:10px verdana;color:#999999" align=right>by <font color=#660000>sTar</font><br> 2003-4-8</td></tr>
</table>
</BODY>
</HTML>

::::站长友情提示:多花一分钟学点什么都好::::

 

上一篇:用javascript作消息提示框(类似于QQ用户上线的消息提示)  下一篇:利用JS获取IE客户端IP及MAC的实现

 ::热点信息::

 

= = 免责声明 = =

① 欢迎转载我网所刊信息,请注明“来源:E天下网络”。
② 凡本网注明“来源:XXX(非E天下网络)”的作品,均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请在30日内进行。
※联系方式:Airtofly@163.com

::推荐文章::

 

Dreamweaver8创建自定义代码片

::图像设计::

 

动态图片搜索家——GIF RUNN
软件搜索利器——FileFerret
实例说明构图要讲规律
Character Builder让你尽展靓
全景图速成者Cool360
三维模型速成工具——Canoma
剪贴专家SmartBoard 32
新世纪的图像处理利器——Ph
更多内容..

 

 

关于我们 广告服务 友情链接 合作伙伴 社区论坛 免责声明

Copyright © 2007   21kn.com Inc. All rights reserved.e天下网络工作室

网站白天客服QQ:26875416 (非24小时)  合作QQ:597004688    粤ICP备06026423号