到网上看了一段树目录代码,想必大部分人都看过这段代码:
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> DSTree </TITLE>
<META NAME="Author" CONTENT="[email protected]" >
<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));
}
//createTree(thisn /*树结点*/ , 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"));
}
if(thisn.getAttribute("target") != null){
upobj.setAttribute("target",thisn.getAttribute("target"));
} nodeObj.appendChild(upobj);
nodeObj.insertAdjacentHTML("beforeEnd","<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",(thisn.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://www.blueidea.com" target="box" treeId="4353" />
                <DSTree text="手绘" href="http://www.blueidea.com" target="box" treeId="543543" />
                <DSTree text="灌水" href="http://www.blueidea.com" target="box" treeId="543543" />
            </DSTree>
            <DSTree text="BlueIdea" open="false" href="http://" target="box" treeId="213">
                <DSTree text="DreamWeaver &amp; 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>我的问题是其中这段中的:
if(thisn.hasChildNodes()){
 var i;
 var nodes = thisn.childNodes;
 var cn = document.createElement("span");
  upobj.setAttribute("cn",cn);//设置upobj属性怎么能用创建的span元素呢?况且测试中发现upobj并没有属性cn?

解决方案 »

  1.   

    upobj.setAttribute("cn",cn);这句是在设置cn属性,就是因为没有才设置cn属性的~·
      

  2.   

    upobj就是函数createTree开始时定义的一个元素span:
    var upobj = document.createElement("span");
    一步一步执行到
    upobj.setAttribute("cn",cn);
    应该是设置span的cn属性呀?
    实在是不明白?
      

  3.   

    肯定会有的 开发人员工具里看一下执行完了的html 源文件是看不到了
    cn="[object]" 类似这种
      

  4.   

    一步一步调试也没有看到cn="[object]" 
      

  5.   

    调试结果看不到cn="[object]" 
    http://hi.csdn.net/attachment/201111/24/0_1322123363BSAI.gif
      

  6.   

    调试里也不会有 不然你alert一下getAttribute("cn") 肯定是object
      

  7.   

    嗯!测试alert(upobj.getAttribute("cn"))
    确实是Object
      

  8.   

    哦!
    原来是我落伍了,IE8开发人员工具自带;我还在用IE6;