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

推荐订阅源

Y
Y Combinator Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threat Research - Cisco Blogs
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
U
Unit 42
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
T
Tor Project blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Recorded Future
Recorded Future
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
Latest news
Latest news
GbyAI
GbyAI
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Hacker News: Ask HN
Hacker News: Ask HN
美团技术团队
N
News | PayPal Newsroom
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Hacker News
The Hacker News
The GitHub Blog
The GitHub Blog
V
V2EX
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Security Latest
Security Latest
博客园 - 叶小钗
P
Palo Alto Networks Blog

博客园 - 龍的傳人

再谈代码生成器,拥有自己最适合的代码生成器 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-28 · via 博客园 - 龍的傳人

对上一篇的javascript+css+xml 全选树 代码进行了整理和增加了一些功能,如:取得当前节点的路径,是否显示checkbox大家可以自己研究.

<!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
    
}
        
    .SPAN A:link 
{font-size:12px;text-decoration:none;color: #ffffff}
    .SPAN A:visited 
{font-size:12px;text-decoration:none;color: #000080}
    .SPAN A:active 
{font-size:12px;text-decoration: none;color: #ff8000}
    .SPAN A:hover 
{font-size:12px;text-decoration:none;color: #ff8040;border: 2px solid #ff0000;/*边框颜色*/ }
    
</Style>
    
<script language='javascript'>
    
function LTree(xPath,oPath,cPath,pPath,mPath,dPath)
    
{
        
this.closeImgPath=cPath;        //关闭图标
        this.openImgPath=oPath;            //打开图标
        this.plusImgPath=pPath;            //+号图标
        this.minusImgPath=mPath;        //-号图标
        this.fileImgPath=dPath;            //默认图标
        this.xmlPath=xPath;                //xml文件路径
        this.getXmlObj=function()
        
{
            
var xmlDoc;
            
if(window.ActiveXObject)
            
{
                xmlDoc
=new ActiveXObject("Microsoft.XMLDOM");
            }

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

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

            
return xmlDoc;
        }

    }

    
var str="";    
    LTree.prototype.tree
=function(divid,bool)
    
{
        
var str="<div class=\"list\" id=\"menu1_child\">";
        
var xmlDoc=this.getXmlObj();
        
var root=xmlDoc.documentElement;
        str
+=this.getTreeString(root,bool);        
        str
+="</div>";    
        document.getElementById(divid).innerHTML
=str;
    
    }

    LTree.prototype.getTreeString
=function(root,bool)
    
{
        
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(bool)
                
{    //显示checkbox                                
                    if(node.hasChildNodes)
                    
{//父节点
                        str+="<li id=\"L"+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 class=\"span\" id=\"S"+id+"\" ondblclick=\"setNodeBgColor(this,'"+id+"-1')\"><a href=\"javascript:\">"+name+"</a></SPAN>"
                        LTree.prototype.getTreeString(node,bool)
                        str
+="</li>";
                    }

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

                
else
                
{//不显示checkbox    
                    if(node.hasChildNodes)
                    
{//父节点
                        str+="<li id=\"L"+id+"\"><img onclick=\"showMinusHide(this,'"+id+"-1')\" src=\""+minusImgPath+"\"/><img onclick=\"showImgHide(this,'"+id+"-1')\" src=\""+openImgPath+"\"/><SPAN class=\"span\" id=\"S"+id+"\" ondblclick=\"setNodeBgColor(this,'"+id+"-1')\"><a href=\"javascript:\">"+name+"</a></SPAN>"
                        LTree.prototype.getTreeString(node,bool)
                        str
+="</li>";
                    }

                    
else
                    
{//子节点
                        str+="<li id=\"L"+id+"\" style=\"margin-left:26px;\" ><img src=\""+fileImgPath+"\"/><SPAN class=\"span\" onclick=\"setNodeBgColor(this,'"+id+"')\" id=\"S"+id+"\" ><a  href=\"javascript:\">"+name+"</a></SPAN></li>"
                    }
    
                }
        
            }

            str
+="</ul>"
        }

        
return str;
    }

    
//打开全部
    function showAll(divid,checkbox)
    
{
        
// checkbox=true 显示checkbox 否则:不显示checkbox
        //var root=document.getElementById(divid).firstChild.firstChild.firstChild;//0级
        var root=null;
        
if(checkbox)
        
{
            root
=document.getElementById(divid).firstChild.firstChild.firstChild.childNodes[4];//1级
        }

        
else
        
{
            root
=document.getElementById(divid).firstChild.firstChild.firstChild.childNodes[3];//1级
        }

        getULNode(root,checkbox,
true);
    }

    
//关闭全部
    function closeAll(divid,checkbox)
    
{        
        
//var root=document.getElementById(divid).firstChild.firstChild.firstChild;//0级
        var root;
        
if(checkbox)
        
{
            root
=document.getElementById(divid).firstChild.firstChild.firstChild.childNodes[4];//1级
        }

        
else
        
{
            root
=document.getElementById(divid).firstChild.firstChild.firstChild.childNodes[3];//1级
        }

        getULNode(root,checkbox,
false);
    }

    
function getULNode(rootNode,checkbox,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,checkbox);
                    }

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

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

        }
        
    }

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

            
else
            
{
                thisobj.previousSibling.previousSibling.setAttribute(
"src",closeImgPath);        
                thisobj.previousSibling.previousSibling.setAttribute(
"src",plusImgPath)
            }

        }
        
    }

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

            
else
            
{
                thisobj.previousSibling.previousSibling.setAttribute(
"src",openImgPath);        
                thisobj.previousSibling.previousSibling.setAttribute(
"src",minusImgPath)
            }

        }
        
    }

    
//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)
    
{
        nodePath
="";
        nodePath
=getNodePath(e);//取得当前节点的路径
        alert(nodePath);
        
if(bgObj==null)
        
{
            bgObj
=e;
        }

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

        bgObj.style.background
="#00ffff"
    }

    
//取当前节点的路径
    var nodePath="";
    
function getNodePath(node)
    
{        
        getParentNodePath(node);
        
var ss="";
        
var t=nodePath.substring(0,nodePath.length-2).split('->');
        
for(var i=t.length-1;i>=0;i--)
        
{
            ss
+=t[i]+"->";
        }

        
return ss.substring(0,nodePath.length-2);
    }

    
function getParentNodePath(node)
    
{
        nodePath
+=node.innerText+"->";    
        
var parNode=node.parentNode.parentNode.previousSibling;//span/li/ul/span
        if(parNode!=null)
        
{
            getParentNodePath(parNode)
        }

    }

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

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

        bgObj.style.background
="red"
        
var obj=document.getElementById(id);        
        
if(obj.style.display=="")
        
{
            obj.style.display
="none";
            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)
        }

    }

    
//*******************************************************下面是调用***********************************************************************//
    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 Test()
    
{    
        
//xPath,oPath,cPath,pPath,mPath,dPath
        var tree=new LTree(xmlPath,openImgPath,closeImgPath,plusImgPath,minusImgPath,fileImgPath);
        tree.tree(
"idmenu");
    }

    
</script>
</head>

<body onload="Test()">

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