<html>
<head>
<title>Tree View</title>
<meta name="author" content="" />
<style type="text/css">
body{
font-size: 9pt;
}
.treeview-line-cross{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif);
background-repeat:no-repeat;
background-position:0px 0px;
float:left;
}
.treeview-line-single{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif);
background-repeat:no-repeat;
background-position:0px -32px;
float:left;
}
.treeview-line-last{
font-size:0px;
border:solid red 0px;
height:8px;
width:16px;
background:url(https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif) left bottom no-repeat;
float:left;
}
.treeview-line-blank{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
float:left;
}
</style>
</head>
<body>
<div id="dest" style="border: solid red 1px; width: 300px; height: 500px;"></div>
</body>
<script>
function $(id){return document.getElementById(id);}
/**
* js树形菜单
* 作者 sunxing007
* 转载请注明来自:http://blog.csdn.net/sunxing007
*/
function TreeView(title, isFolder){
this.el = null;
this.title = title;
this.isFolder = isFolder;
this.children = [];
}
TreeView.prototype.printout = function(ident){
alert(this.title);
if(this.isFolder&&this.children.length>0){
for(var i=0; i<this.children.length; i++){
this.children[i].printout();
}
}
}
TreeView.prototype.toHTML = function(leftStr, append){
var t = "<div style='height:16px; overflow:hidden;'>";
var hasChild = this.isFolder&&this.children.length>0;
if(this.isFolder){
t += (append + "<img src='https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif' οnclick='doLeafClick(this," + hasChild + ")'/>" + this.title);
}
else{
t += (append + "<img src='https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/file.gif' />" + this.title);
}
t += "</div>";
if(this.isFolder&&this.children.length>0){
t += "<div>";
for(var i=0; i<this.children.length-1; i++){
t += ( leftStr + this.children[i].toHTML(leftStr+"<div class='treeview-line-single'></div>", "<div class='treeview-line-cross'></div>"));
}
t += (leftStr + this.children[i].toHTML(leftStr+"<div class='treeview-line-blank'></div>", "<div class='treeview-line-last'></div>"));
t += "</div>";
}
return t;
}
var root = new TreeView("C:(SYSTEM)", true);
var bea = new TreeView("bea", true);
bea.children.push(new TreeView("logs", true));
bea.children.push(new TreeView("license.bea", false));
root.children.push(bea);
var doc = new TreeView("doc", true);
var basic = new TreeView("Basic", true);
basic.children.push(new TreeView("test.xml", false))
doc.children.push(basic);
root.children.push(doc);
root.children.push(new TreeView("winnt", true));
root.children.push(new TreeView("sys.info", false));
root.children.push(new TreeView("1.sql", false));
//root.printout();
$('dest').innerHTML = root.toHTML("","");
function doLeafClick(e, hasChild){
if(hasChild){
var c = e.parentNode.nextSibling;
if(c.style.display==""){
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
c.style.display="none";
}
else{
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
c.style.display="";
}
}
else{
if(e.src.indexOf("folder-closed.gif")==-1){
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
}
else{
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
}
}
}
</script>
</html>
<html>
<head>
<title>Tree View</title>
<meta name="author" content="" />
<style type="text/css">
body{
font-size: 9pt;
}
.treeview-line-cross{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif);
background-repeat:no-repeat;
background-position:0px 0px;
float:left;
}
.treeview-line-single{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
background-image:url(https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif);
background-repeat:no-repeat;
background-position:0px -32px;
float:left;
}
.treeview-line-last{
font-size:0px;
border:solid red 0px;
height:8px;
width:16px;
background:url(https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/treeview-default-line.gif) left bottom no-repeat;
float:left;
}
.treeview-line-blank{
font-size:0px;
border:solid red 0px;
height:16px;
width:16px;
float:left;
}
</style>
</head>
<body>
<div id="dest" style="border: solid red 1px; width: 300px; height: 500px;"></div>
</body>
<script>
function $(id){return document.getElementById(id);}
/**
* js树形菜单
* 作者 sunxing007
* 转载请注明来自:http://blog.csdn.net/sunxing007
*/
function TreeView(title, isFolder){
this.el = null;
this.title = title;
this.isFolder = isFolder;
this.children = [];
}
TreeView.prototype.printout = function(ident){
alert(this.title);
if(this.isFolder&&this.children.length>0){
for(var i=0; i<this.children.length; i++){
this.children[i].printout();
}
}
}
TreeView.prototype.toHTML = function(leftStr, append){
var t = "<div style='height:16px; overflow:hidden;'>";
var hasChild = this.isFolder&&this.children.length>0;
if(this.isFolder){
t += (append + "<img src='https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif' οnclick='doLeafClick(this," + hasChild + ")'/>" + this.title);
}
else{
t += (append + "<img src='https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/file.gif' />" + this.title);
}
t += "</div>";
if(this.isFolder&&this.children.length>0){
t += "<div>";
for(var i=0; i<this.children.length-1; i++){
t += ( leftStr + this.children[i].toHTML(leftStr+"<div class='treeview-line-single'></div>", "<div class='treeview-line-cross'></div>"));
}
t += (leftStr + this.children[i].toHTML(leftStr+"<div class='treeview-line-blank'></div>", "<div class='treeview-line-last'></div>"));
t += "</div>";
}
return t;
}
var root = new TreeView("C:(SYSTEM)", true);
var bea = new TreeView("bea", true);
bea.children.push(new TreeView("logs", true));
bea.children.push(new TreeView("license.bea", false));
root.children.push(bea);
var doc = new TreeView("doc", true);
var basic = new TreeView("Basic", true);
basic.children.push(new TreeView("test.xml", false))
doc.children.push(basic);
root.children.push(doc);
root.children.push(new TreeView("winnt", true));
root.children.push(new TreeView("sys.info", false));
root.children.push(new TreeView("1.sql", false));
//root.printout();
$('dest').innerHTML = root.toHTML("","");
function doLeafClick(e, hasChild){
if(hasChild){
var c = e.parentNode.nextSibling;
if(c.style.display==""){
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
c.style.display="none";
}
else{
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
c.style.display="";
}
}
else{
if(e.src.indexOf("folder-closed.gif")==-1){
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder-closed.gif";
}
else{
e.src="https://p-blog.csdn.net/images/p_blog_csdn_net/sunxing007/EntryImages/20090611/folder.gif";
}
}
}
</script>
</html>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sunxing007/archive/2009/06/11/4261157.aspx