学习网考试学习资料

Gzu521.com

XML+JS创建树形菜单(1)

Javascript教程   点击:次   发布时间:2006-7-13   【字体: 】   来源:Gzu521.com
贵 州 学 习 网
简单说明:

思路:
从数据岛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.appendchild(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=expand.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",true);
   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"));

下一页
本文共3页: 第 1 [2] [3]

责任编辑:gzu521

网页设计分类
HTML教程
CSS教程
Javascript教程
Dreamweaver教程
FrontPages教程
FireWorks教程
Flash教程
PhotoShop教程
建站知识
分类推荐信息
更多...
大类最新文章
更多...