求js树形源码,最好是“资源管理器”的那种(有文件夹和文件图片最好)

解决方案 »

  1.   

    GOOGLE可以搜到很多的。
    不过建议楼主用jQuery。这个里面有tree的,而且做的很好。
      

  2.   

    最好不要google搜到的,因为那几个都不符合要求,还有div的标签好些,谢谢
      

  3.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>无标题页</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=utf-8">
    <STYLE type=text/css> 
    {
    FONT-SIZE: 12px
    }
    #menuTree A {
    COLOR: #566984; TEXT-DECORATION: none
    }
    </STYLE>
    <SCRIPT src="Left.files/TreeNode.js" type=text/javascript></SCRIPT>
    <SCRIPT src="Left.files/Tree.js" type=text/javascript></SCRIPT>
    <META content="MSHTML 6.00.2900.5848" name=GENERATOR>
    </HEAD>
    <BODY 
    style="BACKGROUND-POSITION-Y: -120px; BACKGROUND-IMAGE: url(../images/bg.gif); BACKGROUND-REPEAT: repeat-x">
    <TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%">
      <TBODY>
        <TR>
          <TD width=10 height=29><IMG src="Left.files/bg_left_tl.gif"></TD>
          <TD 
        style="FONT-SIZE: 18px; BACKGROUND-IMAGE: url(../images/bg_left_tc.gif); COLOR: white; FONT-FAMILY: system">系统菜单</TD>
          <TD width=10><IMG src="Left.files/bg_left_tr.gif"></TD>
        </TR>
        <TR>
          <TD style="BACKGROUND-IMAGE: url(../images/bg_left_ls.gif)"></TD>
          <TD id=menuTree 
        style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; HEIGHT: 100%; BACKGROUND-COLOR: white" 
        vAlign=top></TD>
          <TD style="BACKGROUND-IMAGE: url(../images/bg_left_rs.gif)"></TD>
        </TR>
        <TR>
          <TD width=10><IMG src="Left.files/bg_left_bl.gif"></TD>
          <TD style="BACKGROUND-IMAGE: url(../images/bg_left_bc.gif)"></TD>
          <TD width=10><IMG 
    src="Left.files/bg_left_br.gif"></TD>
        </TR>
      </TBODY>
    </TABLE>
    <SCRIPT type=text/javascript>
    var tree = null;var root = new TreeNode('系统菜单');/*
    var fun1 = new TreeNode('公司管理');
    var fun2 = new TreeNode('机构管理', 'BranchMgr.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun1.add(fun2);
    var fun3 = new TreeNode('部门管理', 'DepartmentMgr.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun1.add(fun3);
    var fun4 = new TreeNode('员工管理', 'EmployeeMgr.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun1.add(fun4);
    root.add(fun1);
    */
    var fun5 = new TreeNode('个人管理');
    var fun6 = new TreeNode('个人管理', 'MySchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun6);
    var fun7 = new TreeNode('个人信息', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun7);
    var fun8 = new TreeNode('修改密码', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun8);
    var fun9 = new TreeNode('写新邮件', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun9);
    var fun10 = new TreeNode('写新日记', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun10);
    var fun11 = new TreeNode('待办事项', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun11);
    var fun12 = new TreeNode('通讯信息', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun12);
    var fun13 = new TreeNode('上传文件', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun13);
    var fun14 = new TreeNode('日记查看', 'MyNote.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun5.add(fun14);
    root.add(fun5);var fun15 = new TreeNode('文档管理');
    var fun16 = new TreeNode('文档管理', 'DocumentMgr.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun15.add(fun16);
    var fun17 = new TreeNode('文件浏览', 'DeptSchedule.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun15.add(fun17);
    var fun18 = new TreeNode('回收站', 'Recycler.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun15.add(fun18);
    var fun19 = new TreeNode('文章搜索', 'FileSearch.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun15.add(fun19);
    root.add(fun15);var fun20 = new TreeNode('消息传递');
    var fun21 = new TreeNode('消息管理', 'MessageMgr.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun20.add(fun21);
    var fun22 = new TreeNode('信箱', 'MailBox.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun20.add(fun22);
    root.add(fun20);
    var fun23 = new TreeNode('系统管理');
    var fun24 = new TreeNode('菜单管理', 'MenuSort.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun23.add(fun24);
    var fun25 = new TreeNode('角色管理', 'RoleMgr.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun23.add(fun25);
    var fun26 = new TreeNode('登录日志', 'LoginLog.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun23.add(fun26);
    var fun27 = new TreeNode('操作日志', 'OperationLog.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun23.add(fun27);
    root.add(fun23);tree = new Tree(root);tree.show('menuTree')
    //var fun20 = new TreeNode('菜单排序', 'MenuSort.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun16.add(fun20);root.add(fun16);//var fun21 = new TreeNode('考勤管理');
    //var fun22 = new TreeNode('签到签退', 'SignInOrOut.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun21.add(fun22);
    //var fun23 = new TreeNode('考勤查询', 'HistoryQuery.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun21.add(fun23);
    //var fun24 = new TreeNode('考勤统计', 'TimeStatistics.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun21.add(fun24);root.add(fun21);tree = new Tree(root);tree.show('menuTree')
    </SCRIPT>
    </BODY>
    </HTML>
      

  4.   

    Tree.jsfunction Tree(rootNode) {
    var $ = this;
    this.root = rootNode;

    this.show = function(container) {
    $.update($.root);
    this.root.expand();

    if(container.tagName)
    container.appendChild($.root.container);
    else if(typeof container == "string")
    document.getElementById(container).appendChild($.root.container);
    }

    this.update = function(parent) {
    parent.indent();
    for (var i = 0; i < parent.children.length; i++) {
    parent.children[i].level = parent.level + 1;
    for (var j = 0; j < parent.ancestor.length; j++) {
    parent.children[i].ancestor.push(parent.ancestor[j]);
    }
    parent.children[i].ancestor.push(parent);
    $.update(parent.children[i]);
    }
    }
    }
    TreeNode.jsvar currentNode = null;function TreeNode(text, url, iconOpen, iconOpenHover, iconClosed, iconClosedHover) {
    var $ = this;
    this.level = 0;
    this.children = [];
    this.parent = null;
    this.status = "CLOSED";
    this.ancestor = [];
    this.isHover = false;

    this.PATH = "../images/tree/";
    this.COLLAPSED = this.PATH + "arrow_collapsed.gif";
    this.EXPANDED = this.PATH + "arrow_expanded.gif";
    this.COLLAPSED_HOVER = this.PATH + "arrow_collapsed_hover.gif";
    this.EXPANDED_HOVER = this.PATH + "arrow_expanded_hover.gif";
    this.CATEGORYOPEN = this.PATH + (iconOpen ? iconOpen : "folder_open.gif");
    this.CATEGORYOPEN_HOVER = this.CATEGORYOPEN;
    this.CATEGORYCLOSED = this.PATH + (iconClosed ? iconClosed : "folder_closed.gif");
    this.CATEGORYCLOSED_HOVER = this.CATEGORYCLOSED;
    this.EMPTY = this.PATH + "empty.gif"; this.container = document.createElement("DIV");
    this.content = document.createElement("DIV");
    this.indentSpace = document.createElement("SPAN");
    this.statusIcon = document.createElement("IMG");
    this.node = document.createElement("SPAN");
    this.nodeIcon = document.createElement("IMG");
    this.label = document.createElement("A");
    this.container.appendChild(this.content);
    this.content.appendChild(this.indentSpace);
    this.content.appendChild(this.statusIcon);
    this.content.appendChild(this.node);
    this.node.appendChild(this.nodeIcon);
    this.node.appendChild(this.label); this.container.style.display = "block";
    this.statusIcon.src = this.COLLAPSED;
    this.nodeIcon.src = this.CATEGORYCLOSED;
    this.nodeIcon.align = "absmiddle";
    this.statusIcon.align = "absmiddle";
    this.statusIcon.style.cursor = "default";
    this.node.style.cursor = "default";
    this.label.style.lineHeight = "20px";
    this.label.style.fontSize = "12px";
    this.label.style.display = "inline-block";
    this.label.style.backgroundImage = "url(" + this.BG + ")";
    this.label.style.backgroundRepeat = "repeat-x";
    this.label.innerHTML = text;

    if (url) {
        this.label.href = url;
        this.label.target = "mainFrame";
    } this.add = function(child) {
    this.container.appendChild(child.container);
    this.children.push(child);
    child.parent = this;
    }

    this.remove = function(child) {
    child.container.removeNode(true);
    var temp = [];
    for (var i = 0; i < this.children.length; i++) {
    if (this.children[i] != child) {
    temp.push(this.children[i]);
    } else {
    continue;
    }
    }
    this.children = temp;
    } this.hidden = function() {
    this.container.style.display = "none";
    } this.show = function() {
    this.container.style.display = "block";
    } this.getAncestor = function(level) {
    if (this.level == level)
    return this;
    for (var i = 0; i < $.ancestor.length; i++) {
    if ($.ancestor[i].level == level) {
    return $.ancestor[i];
    }
    }
    return null;
    }

    this.contains = function(node) {
    for (var i = 0; i < $.children.length; i++) {
    if ($.children[i] == node) {
    return true;
    }
    $.children[i].contains(node);
    }
    return false;
    }

    this.indent = function() {
    this.indentSpace.innerHTML = "";
    for (var i = 0; i < this.level; i++) {
    var indentImg = document.createElement("IMG");
    indentImg.src = this.EMPTY;
    indentImg.align = "absmiddle";
    this.indentSpace.appendChild(indentImg);
    }
    this.collapse();
    }

    this.setIcon = function() {
    this.nodeIcon = this.status == "CLOSED" ? 
    ($.isHover ? $.CATEGORYCLOSED_HOVER : $.CATEGORYCLOSED) : 
    ($.isHover ? $.CATEGORYOPEN_HOVER : $.CATEGORYOPEN);
    }

    this.collapse = function() {
    for (var i = 0; $.children && i < $.children.length; i++) {
    $.children[i].hidden();
    }
    $.statusIcon.src = $.COLLAPSED;
    $.nodeIcon.src = $.CATEGORYCLOSED;
    $.status = "CLOSED";
    }

    this.expand = function() {
    for (var i = 0; $.children && i < $.children.length; i++) {
    $.children[i].show();
    }
    $.statusIcon.src = $.EXPANDED;
    $.nodeIcon.src = $.CATEGORYOPEN;
    $.status = "OPEN";
    }

    this.expandOrCollapse = function() {
    if ($.status == "CLOSED") {
    if (currentNode) {
    var ancestor = currentNode.getAncestor(1);
    var myAncestor = $.getAncestor(1);
    if (ancestor && myAncestor && ancestor != myAncestor) {
    ancestor.collapse();
    }
    }
    currentNode = $;
    $.expand();
    } else {
    $.collapse();
    }
    } this.node.onmousedown = function() {
    if (currentNode) {
    currentNode.nodeIcon.src = (currentNode.status == "CLOSED" ? currentNode.CATEGORYCLOSED : currentNode.CATEGORYOPEN);
    }
    }

    this.node.onmouseup = function() {
    if (event.button == 2) {

    }
    }

    this.content.onselectstart = function() {
    return false;
    } this.statusIcon.onclick = this.expandOrCollapse;
    this.nodeIcon.ondblclick = this.expandOrCollapse;
    this.label.onclick = this.expandOrCollapse;

    this.statusIcon.onmouseover = function() {
    this.src = $.status == "CLOSED" ? $.COLLAPSED_HOVER : $.EXPANDED_HOVER;
    }

    this.statusIcon.onmouseout = function() {
    this.src = $.status == "CLOSED" ? $.COLLAPSED : $.EXPANDED;
    }
    }
      

  5.   


    <style type="text/css"><!--
    .pnode { white-space:nowrap;}
    .cnode { white-space:nowrap;color:#000;}
    .RootTree {                
            padding:0px;        
            font-size:12px;
            font-family:"宋体";
            text-align:left;
            width:120px;
            margin:0 auto;
            height:100%; 
            color:#000000;
            }
    .RootTree a {padding:2px 1px 3px 1px; color:#000000;text-decoration:none;}
    .RootTree a:hover {color:#d2c29a;  }
    .RootTree a:active {color:#000000;}
    -->
    </style><script language="javascript">var Prealink=-1,Pcollapse=0;
    var iconblank="http://p1.images22.51img1.com/6000/milesj/184bce4d78b4b472a46ceefa39af2a21.gif";
    var iconcollapse="/data/upload/remote/20090826/1606350.gif";
    var iconexpand="/data/upload/remote/20090826/zjj19810518.gif";
    var iconaccout="http://pc.images22.51img1.com/6000/milesj/c733f2f267328460163dc14bcfb31bf7.gif";
    var iconfolder="http://pa.images22.51img1.com/6000/milesj/abafcbc910b1f38dae08c534c274a7af.gif";
    var iconfolderopen="http://pc.images22.51img1.com/6000/milesj/c8d7380a13237d65ee1fbfe68fba28a8.gif";
    //-------------------- 左侧目录树js部分 ---------------
    function TreeExpand(eimg) {//--树的展开-与收起+
    if (eimg.alt=="+") {
        eimg.alt="-";
        eimg.src=iconexpand;
        eimg.parentNode.parentNode.getElementsByTagName("span")[0].style.display="block";
        }
    else {
        eimg.alt="+";
        eimg.src=iconcollapse;
        eimg.parentNode.parentNode.getElementsByTagName("span")[0].style.display="none";
        var Curalink=eimg.parentNode.getElementsByTagName("a")[0]
        if (Prealink == -1 || Prealink == Curalink)        {return;}
        var parentTree,CurTree;//--以下,折叠位置是原聚焦节点的父节点,则触发父节点的点击
        parentTree=Prealink.parentNode.parentNode;
        CurTree=eimg.parentNode.parentNode;
        while(parentTree.className == "ChildTree" || parentTree.className == "RootTree"){
            if(CurTree == parentTree){
                Pcollapse=1;
                imgFolder(eimg);
                break;}
            parentTree=parentTree.parentNode.parentNode;        
            }    
        }//endelse
    }
    function imgFolder(fimg) {//--目录的点击 即是触发链接A
    var alink=fimg.parentNode.getElementsByTagName("a")[0]
    alink.focus();
    //open (alink.href, alink.target ?alink.target : '_blank');   //--模拟a.click()方法以兼容firefox
    if(alink.name == "accountlink")        aFolder(alink,0);
    else    aFolder(alink,1);
    }
    function aFolder(fa,i) {//--链接的点击-目录打开及展开子树 根i=0 子i=1
    var expandimg=fa.parentNode.getElementsByTagName("img")[0];
    //--前节点目录图标关闭
    if(Prealink != -1 && Prealink.name != "accountlink")    
        {Prealink.parentNode.getElementsByTagName("img")[1].src=iconfolder;}
    //--当前节点目录图标打开;根i=0,子i=1
    if(i) 
        {fa.parentNode.getElementsByTagName("img")[1].src=iconfolderopen;}
    //--目录树的展开
    if(Pcollapse==0 && expandimg.alt=="+"){
        var expandimg=fa.parentNode.getElementsByTagName("img")[0];
        expandimg.alt="-";
        expandimg.src=iconexpand;
        fa.parentNode.parentNode.getElementsByTagName("span")[0].style.display="block";
        }
    else    {Pcollapse=0;}
    Prealink=fa;
    }</script>html代码
    [code=HTML]
    <div class="RootTree">
        
    <div class="pnode">
            <img onclick="TreeExpand(this)" alt="-" src="data/images/main/zjj19810518.gif" align="absMiddle" name="expandimg" /> 
                <img height="20" src="data/images/main/myxgcp.jpg" width="56" />
         </div>
         <span class="treespan" style="DISPLAY:block; MARGIN-LEFT: 19px"><!-----child tree s---
    -->        
    <div class="ChildTree">
                
    <div class="cnode">
                    <img onclick="TreeExpand(this)" alt="+" src="/data/upload/remote/20090826/1606350.gif" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="index.php?id=347" target="_blank" title="Garments">Garments</a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"><!-----child tree s---
    -->                
    <div class="ChildTree">
                        
    <div class="cnode">
                            <img src="data/images/main/leftzc.jpg" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="index.php?id=358" target="_blank" title="Men's Apparel">Men's Apparel</a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"></span></div><!--child tree e
    -->                
    <div class="ChildTree">
                        
    <div class="cnode">
    <img src="data/images/main/leftzc.jpg" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="product.php?classid=16" target="_blank" title="Women's Apparel">Women's Apparel</a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"></span></div><!--child tree e
    -->                
    <div class="ChildTree">
                        
    <div class="cnode">
                            <img src="data/images/main/leftzc.jpg" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="#" target="_blank" title="Underwear & Swimwear">Underwear... </a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"></span></div><!--child tree e
    -->                
    <div class="ChildTree">
                        
    <div class="cnode">
                            <img src="data/images/main/leftzc.jpg" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="#" target="_blank" title="Kids' Wear">Kids' Wear</a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"></span></div><!--child tree e
    -->                
    <div class="ChildTree">
                        
    <div class="cnode">
                            <img src="data/images/main/leftzc.jpg" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="#" target="_blank" title="Sports and Casual Wear">Sports...</a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"></span></div><!--child tree e
    -->                
                    
    <div class="ChildTree">
                        
    <div class="cnode">
                            <img src="data/images/main/leftzc2.jpg" align="absMiddle" name="expandimg" /> <a onclick="aFolder(this,1)" href="#" target="_blank" title="Other Wear">Other Wear</a> </div><span class="treespan" style="DISPLAY: none; MARGIN-LEFT: 19px"></span></div><!--child tree e
    -->
    </span></div><!--child tree one end
    -->图片可以自己定义。只要路径对就好了
    [/code]
      

  6.   

    参见dhATV4:
    http://www.easyui.org.cn/dhatv_demo.html