惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
罗磊的独立博客
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
S
Schneier on Security
V
Vulnerabilities – Threatpost
T
Tenable Blog
博客园 - 【当耐特】
博客园 - 聂微东
I
Intezer
S
Securelist
C
Cisco Blogs
腾讯CDC
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
Security Archives - TechRepublic
Security Archives - TechRepublic
D
Darknet – Hacking Tools, Hacker News & Cyber Security
美团技术团队
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI
Cloudbric
Cloudbric
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
The Last Watchdog
The Last Watchdog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
C
CERT Recently Published Vulnerability Notes
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
The Register - Security
The Register - Security
博客园 - 叶小钗
博客园 - 司徒正美
博客园_首页
Google DeepMind News
Google DeepMind News
Hacker News: Ask HN
Hacker News: Ask HN
Spread Privacy
Spread Privacy
雷峰网
雷峰网
I
InfoQ
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Schneier on Security
Schneier on Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 龍的傳人

再谈代码生成器,拥有自己最适合的代码生成器 javascript+css+xml 全选树(再续) 设表格细钱 javascript+css+xml 全选树(续) 白话CMMI 如何做好需求收集[来之《程序员》第2期] javascript、CSS、XML动太生成树菜单 - 龍的傳人 - 博客园 可选择也可填写的下拉框(用鼠标\键盘的上下键选择) - 龍的傳人 - 博客园 兼容firefox和IE的两级联动下拉菜单的javascript代码 HTML 图片分析 JavaScript - Import XML Document firefox与IE对javascript和CSS的区别(转) IE和Firefox在JavaScript方面的兼容性(转) javascript在中ie与firefox的区别与解决方案(转) 针对Firefox兼容性,要注意的一些问题 (转) sql2000和文本文件的写入和读取(转) [转]JS代码格式化工具(附源代码) ASP.NET2.0调用MySql的存储过程 Remote建立分析
javascript+css+xml 全选树
龍的傳人 · 2008-03-27 · via 博客园 - 龍的傳人

vs2005 的TreeView树控件入门非常简单,花费很少时间就可以构造一颗树形目录来,是新手的不错选择,但是它缺少灵活性,要改变它的图标除了它本身的模板外,就有点麻烦了,还有它的点击回发事件,特别烦。如果选择父节点同时选择子节点,就很麻烦了,因为它的结构是div+table的,我们要扩展它也要花不少时间。我发现很多高手都没有用到它,都是自己写的,这样可以灵活扩展,和修改相关属性,下面是我用javascript+css+xml写的一颗树,只要xml文件的格式不变,你可以随便增加、删除目录,很容易就可以得到相应的树目录了,你可以在原有的基础上扩展更多的属性、和相关的操作。当然刚刚写出来的东西不是很好,还有BUG,没有时间测试,如果大家有什么建议或发现bug请提出来。大家一起讨论。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    
<title>Untitled</title>
    
<Style>
/*    .on
    {
        background:#5eeDBE url(images/menu_head_bg.gif) repeat-x;
        text-decoration:none;
        border: 1px solid #2a4dab;
        font:bold 12px/22px "lucida Grande", verdana, lucida, Arial, helvetica, "宋体", sans-serif;
        
    }
*/

    .list
{margin:0;padding:0;}
    .list ul
    
{
        
/*background:#5eeDBE url(images/menu_head_bg.gif) repeat-x;背景样式依次为:颜色,图片路径,横向重复*/
        list-style-type
:none;    
        margin
:0,0,0,9;
        padding
:0;
        padding-left
:1px;    
    
}

    .list li
    
{
        font
:bold 12px/22px "lucida Grande", verdana, lucida, Arial, helvetica, "宋体", sans-serif;/*文字样式依次为:粗体 大小/行高 字族*/
        list-style-type
:none;
        margin
:0;
        padding
:0;
        padding-left
:12px;
        cursor
:hand;
        background
:#008080
    
}


    A:link 
{font-size:12px;text-decoration:none;color: #FFFFFF}
    A:visited 
{font-size:12px;text-decoration:none;color: #FFFFFF}
    A:active 
{font-size:12px;text-decoration: none;color: ##FFFFFF}
    A:hover 
{font-size:12px;text-decoration:none;color: #ff00ff;border: 1px solid #2a4dab;/*边框颜色*/ }

    
</Style>
    
<script language='javascript'>
    
function GetName()
    
{
        alert(
"88888");
    }

    
var closeImgPath="C:\\Documents and Settings\\kevin.long\\桌面\\Web\\books_close.gif";    //关闭图标
    var openImgPath="C:\\Documents and Settings\\kevin.long\\桌面\\Web\\books_open.gif";    //打开图标
    var plusImgPath="C:\\Documents and Settings\\kevin.long\\桌面\\Web\\plus.gif";            //+号图标
    var minusImgPath="C:\\Documents and Settings\\kevin.long\\桌面\\Web\\minus.gif";        //-号图标
    var fileImgPath="C:\\Documents and Settings\\kevin.long\\桌面\\Web\\icon_text.gif";        //默认图标
    var xmlPath="C:\\Documents and Settings\\kevin.long\\桌面\\Web\\Tree.xml";
    
    
    
function show(thisobj)
    
{
        
        alert(thisobj);
        
    }

    
</script>
    
<script language='javascript'>
    
var xmlDoc;
    
function CreateXmlObj()
    
{
        
if(window.ActiveXObject)
        
{
        xmlDoc
=new ActiveXObject("Microsoft.XMLDOM");
        }

        
else
        
{
            xmlDoc
==document.implementation.createDocument("","",null);
        }

        
//xmlDoc.async=false;
        xmlDoc.load(xmlPath);
        
if(xmlDoc.parseError.errorCode!=0)
        
{
            
var xmlErr=xmlDoc.parseError;
            alert(
"出错:"+xmlErr.reason);
        }

        
    }

    
//父节点事件
    var bgObj=null;
    
function showhide(thisobj,id)
    
{
        
if(bgObj==null)
        
{
            bgObj
=thisobj;
        }

        
else
        
{
            bgObj.style.background
=""
            bgObj
=thisobj;            
        }

        bgObj.style.background
="red"
        
//alert(thisobj.outerHTML);
        var obj=document.getElementById(id);        
        
//alert(thisobj.previousSibling.previousSibling.getAttribute("src"));
        if(obj.style.display=="")
        
{
            obj.style.display
="none";
            
//thisobj.innerText="+"+thisobj.innerText.substring(1);
            thisobj.previousSibling.previousSibling.setAttribute("src",closeImgPath);        
            thisobj.previousSibling.previousSibling.previousSibling.setAttribute(
"src",plusImgPath)
        }

        
else
        
{
            obj.style.display
="";
            
//thisobj.innerText="-"+thisobj.innerText.substring(1);
            thisobj.previousSibling.previousSibling.setAttribute("src",openImgPath)
            thisobj.previousSibling.previousSibling.previousSibling.setAttribute(
"src",minusImgPath)
        }

    }

    
//打开、关闭文件夹图标事件
    function showImgHide(thisobj,id)
    
{
        
        
var obj=document.getElementById(id);        
        
if(obj.style.display=="")
        
{
            obj.style.display
="none";
            thisobj.setAttribute(
"src",closeImgPath);        
            thisobj.previousSibling.setAttribute(
"src",plusImgPath)
        }

        
else
        
{
            obj.style.display
="";
            thisobj.setAttribute(
"src",openImgPath)
            thisobj.previousSibling.setAttribute(
"src",minusImgPath)
        }

    }

    
//+、-符号事件
    function showMinusHide(thisobj,id)
    
{
        
        
var obj=document.getElementById(id);        
        
if(obj.style.display=="")
        
{
            obj.style.display
="none";
            thisobj.setAttribute(
"src",plusImgPath);        
            thisobj.nextSibling.setAttribute(
"src",closeImgPath)
        }

        
else
        
{
            obj.style.display
="";
            thisobj.setAttribute(
"src",minusImgPath)
            thisobj.nextSibling.setAttribute(
"src",openImgPath)
        }

    }

    
//checkbox 全选事件
    function checkAll(e)
    
{
        
var bool=e.checked;        
        
var childObj=document.getElementById(e.id+"-1");        
        setCheckBox(bool,childObj);        
    }

    
function setCheckBox(bool,chk)
    
{        
        
if(chk!=null)
        
{        
            
//alert(chk.childNodes.length);
            if(chk.childNodes.length>0)
            
{
                
for(var i=0;i<chk.childNodes.length;i++)
                
{
                    
//alert(chk.childNodes[i].type);
                    if(chk.childNodes[i].type=="checkbox")
                    
{            
                        chk.childNodes[i].setAttribute(
"checked",bool);
                    }

                    
                    
if(chk.childNodes[i].childNodes.length>0)
                    
{
                        setCheckBox(bool,chk.childNodes[i]);
                    }

                }

            }

        }
    
    }

    
//设置节点背景颜色
    function setNodeBgColor(e,id)
    
{
        
if(bgObj==null)
        
{
            bgObj
=e;
        }

        
else
        
{
            bgObj.style.background
=""
            bgObj
=e;            
        }

        bgObj.style.background
="#804000"
    }

    
var str="<div class=\"list\" id=\"menu1_child\" title=\"菜单功能区\">";
    
function CreateTreeNode(root)
    
{
        
if(root.hasChildNodes)
        
{
            
var uid=root.getAttribute("Id")+"-1";
            str
+="<ul id=\""+uid+"\">"
            
for(var i=0;i<root.childNodes.length;i++)
            
{
                
var node=root.childNodes[i];
                
var id=node.getAttribute("Id");
                
var name=node.getAttribute("Name");                                            
                
if(node.hasChildNodes)
                
{//父节点
                    str+="<li id=\""+id+"\"><img onclick=\"showMinusHide(this,'"+id+"-1')\" src=\""+minusImgPath+"\"/><img onclick=\"showImgHide(this,'"+id+"-1')\" src=\""+openImgPath+"\"/><input onclick=\"checkAll(this)\" type=\"checkbox\" id=\""+id+"\" /><SPAN id=\"S"+id+"\" onclick=\"showhide(this,'"+id+"-1')\"><a >"+name+"</a></SPAN>"
                    CreateTreeNode(node)
                    str
+="</li>";
                }

                
else
                
{//子节点
                    str+="<li id=\""+id+"\" style=\"margin-left:18px;\" ><img src=\""+fileImgPath+"\"/><input  type=\"checkbox\" id=\""+id+"\" /><a onclick=\"setNodeBgColor(this,'"+id+"')\" href=\"javascript:\">"+name+"</a></li>"
                }
                
            }

            str
+="</ul>"
        
//    alert(root.childNodes.length);
        }

    }

    
//打开全部
    function showAll()
    
{
        
//var root=document.getElementById("idmenu").firstChild.firstChild.firstChild;//0级
        var root=document.getElementById("idmenu").firstChild.firstChild.firstChild.childNodes[4];//1级
        getULNode(root,true);
    }

    
//关闭全部
    function closeAll()
    
{        
        
//var root=document.getElementById("idmenu").firstChild.firstChild.firstChild;//0级
        var root=document.getElementById("idmenu").firstChild.firstChild.firstChild.childNodes[4];//1级
        getULNode(root,false);
    }

    
function getULNode(rootNode,bool)
    
{
        
for(var i=0;i<rootNode.childNodes.length;i++)
        
{
            
if(rootNode.childNodes[i].hasChildNodes)
            
{
                
if(rootNode.childNodes[i].tagName=="UL")
                
{                    
                    
if(bool)
                    
{
                        openUL(rootNode.childNodes[i].previousSibling,rootNode.childNodes[i].id);
                    }

                    
else
                    
{
                        closeUL(rootNode.childNodes[i].previousSibling,rootNode.childNodes[i].id);
                    }

                }
            
                getULNode(rootNode.childNodes[i],bool);                
            }

        }
        
    }

    
function closeUL(thisobj,id)
    
{    
        
var obj=document.getElementById(id);        
        
if(obj!=null)
        
{
            obj.style.display
="none";
            thisobj.previousSibling.previousSibling.setAttribute(
"src",closeImgPath);        
            thisobj.previousSibling.previousSibling.previousSibling.setAttribute(
"src",plusImgPath)
        }
        
    }

    
function openUL(thisobj,id)
    
{    
        
var obj=document.getElementById(id);        
        
if(obj!=null)
        
{
            obj.style.display
="";
            thisobj.previousSibling.previousSibling.setAttribute(
"src",openImgPath);        
            thisobj.previousSibling.previousSibling.previousSibling.setAttribute(
"src",minusImgPath)
        }
        
    }

    
function Test()
    
{
        CreateXmlObj();
        
//alert(xmlDoc.documentElement.firstChild.getAttribute("Name"));
        var root=xmlDoc.documentElement;
        
//alert(root.xml);
        CreateTreeNode(root);
        str
+="</div>";
        
//alert(str);
        document.getElementById("codeText").value=str
        idmenu.innerHTML
=str;
    }

    
</script>
</head>

<body onload="Test()">
<input type="button" onclick="showAll()" value="全部展開">
<input type="button" onclick="closeAll()" value="全部關閉">
<input type="button" onclick="Test()" value="测试">
<input id="codeText" type="text" value="">
<div id="idmenu">555</div>
</body>
</html>