return $tree; }// Create a node for each child in the xml. UiFactory.createTreeNode = function($nodeContent, $parent) { var text = $nodeContent.attr("text"); if (typeof(text) == "undefined") { return; }
var $ul = $("<ul><a></a></ul>").appendTo($parent); var $a = $("a", $ul); text = $.base64Decode(text); $a.text(text);
// If has children, iteratively walk through the children. if ($nodeContent.children().length > 0) { $nodeContent.children().each(function() { UiFactory.createTreeNode($(this), $ul); }); } } 树使用的css:.tree a { text-decoration: none; cursor: default; }.tree ul { padding-left: 2px; margin: 0 2px 0 30px; }.tree > ul { /* Remove the blank at first child of the tree*/ margin-left: 2px; }.tree .hlt { text-shadow: 0px 1px 1px white; background-image:-moz-linear-gradient(#F2EC00, #EDCF00); background-image:-webkit-gradient(linear, left top, left bottom, from(#F2EC00), to(#EDCF00));
var $tree = UiFactory.createTree($(xmlData));
$("body").append($tree);
}
生成树的js函数:/******************************************************************************
* Create tree using xml tree content.
*****************************************************************************/
// <ul><a>A1</a>
// ....<ul><a>A1's child a</a></ul>
// ....<ul><a>A1's child b</a></ul>
// </ul>
// <ul><a>A1' sibling 1</a></ul>
// <ul><a>A1' sibling 2</a></ul>
UiFactory.createTree = function($xmlTreeContent) {
var $tree = $("<div class='tree'>");
$xmlTreeContent.children(0).children().each(function() {
UiFactory.createTreeNode($(this), $tree);
});
// Add leaf's and branch's css style.
$("ul", $tree).each(function() {
if ($(this).children("ul").length == 0) {
$(this).addClass("leaf");
} else {
$(this).addClass("branch");
}
});
// Handle branch mouse event.
$(".branch", $tree).click(function(event) {
$(this).children("ul").slideToggle(100);
return false;
}).mouseover(function() {
$(this).addClass("hlt");
return false;
}).mouseout(function() {
$(this).removeClass("hlt");
return false;
});
// Handle leaf mouse event.
$(".leaf", $tree).click(function() {
return false;
}).mouseover(function() {
return false;
}).mouseout(function() {
return false;
});
return $tree;
}// Create a node for each child in the xml.
UiFactory.createTreeNode = function($nodeContent, $parent) {
var text = $nodeContent.attr("text");
if (typeof(text) == "undefined") { return; }
var $ul = $("<ul><a></a></ul>").appendTo($parent);
var $a = $("a", $ul);
text = $.base64Decode(text);
$a.text(text);
// If has children, iteratively walk through the children.
if ($nodeContent.children().length > 0) {
$nodeContent.children().each(function() {
UiFactory.createTreeNode($(this), $ul);
});
}
}
树使用的css:.tree a {
text-decoration: none;
cursor: default;
}.tree ul {
padding-left: 2px;
margin: 0 2px 0 30px;
}.tree > ul { /* Remove the blank at first child of the tree*/
margin-left: 2px;
}.tree .hlt {
text-shadow: 0px 1px 1px white;
background-image:-moz-linear-gradient(#F2EC00, #EDCF00);
background-image:-webkit-gradient(linear, left top, left bottom, from(#F2EC00), to(#EDCF00));
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
-moz-box-shadow: 0px 1px 5px #999;
-webkit-box-shadow: 0px 1px 5px #999;
box-shadow: 0px 1px 5px #999;
}.tree .branch {
font-weight: bold;
}.tree .branch > a {
cursor: pointer;
padding-left: 16px;
background: url(../images/folder.png) no-repeat left center;
}.tree .leaf {
color: gray;
font-weight: lighter;
}
树的xml数据格式:<?xml version="1.0"?>
<root text="country" href="">
<node text="Germany">
<node text="Braunschweig" href="">
<node text="TU-BS" href="http://www.tu-braunschweig.de/"/>
<node text="Rathaus" href="http://en.wikipedia.org/wiki/Rathaus"/>
</node>
<node text="Magdeburg" href=""/>
<node text="Hannover" href="">
<node text="TU-Hannover" href=""/>
<node text="Rathaus" href=""/>
</node>
<node text="Peking" href="">
<node text="Uni-Qinghua" href="http://www.tu-braunschweig.de/"/>
<node text="Uni-Beda" href="http://en.wikipedia.org/wiki/Rathaus"/>
</node>
</node>
<node text="China" href="">
<node text="Peking" href="">
<node text="Uni-Qinghua" href="http://www.tu-braunschweig.de/"/>
<node text="进阳" href="">
<node text="Uni-Beda" href="http://en.wikipedia.org/wiki/Rathaus"/>
</node>
<node text="Uni-Beda" href="http://en.wikipedia.org/wiki/Rathaus"/>
</node>
</node>
</root>