因为使用的是单个XML由于没有实现通过GETELEMENTBYID获得ID所以实现起来没有达到异步效果
TREEVIEW.JS代码帖出/*under prototype.js*/
function TreeView(){
this.TreeName = "TreeView"; //TreeView名字
this.TreeID = "id" //树节点ID
this.TitleStr = "title" //节点标题
this.ContentStr = "content"; //节点内容
this.UrlStr = "url"; //节点超链接
this.RequestUrl = null; //数据路径
this.DemoID = null; //TreeView容器名
this.DataSource = null; //数据源
this.xmlText = ""; //数据内容
this.NodeID = ""; //节点ID
this.HeaderStr = "<div class='opened'><ul><li><img src='images/opened.gif' />{$TreeName}</li>";
this.ElementBeginStr = "<li><img id='img{$TreeID}' src='images/closed.gif' style='cursor:hand;' title='展开' onclick=\"javascript:Tree('{$TreeID}');\" />{$TitleStr}</li><span id='span{$TreeID}' class='closed'>";
this.ElementStr = "<li><img src='images/node.gif'><a href='{$UrlStr}' title='{$ContentStr}'>{$TitleStr}</a></li>";
this.ElementEndStr = "</span>";
this.FooterStr = "</ul></div>";
}
TreeView.prototype.DataBind = function(ds){
for(var i=0;i<ds.length;i++)
{
if(ds[i].hasChildNodes())
{
this.xmlText += this.ElementBeginStr.replace(/\{\$TreeID\}/g,ds[i].getAttribute(this.TreeID)).replace(/\{\$TitleStr\}/g,ds[i].getAttribute(this.TitleStr));
this.DataBind(ds[i].childNodes);
this.xmlText += this.ElementEndStr;
}
else
{
this.xmlText += this.ElementStr.replace(/\{\$UrlStr\}/g,ds[i].getAttribute(this.UrlStr)).replace(/\{\$ContentStr\}/g,ds[i].getAttribute(this.ContentStr)).replace(/\{\$TitleStr\}/g,ds[i].getAttribute(this.TitleStr));
}
}
};
Tree = function(DemoID){
if($('span'+DemoID).className == 'opened')
{
$('img'+DemoID).src = 'images/closed.gif';
$('img'+DemoID).title = '展开';
$('span'+DemoID).className = 'closed';
}
else
{
$('img'+DemoID).src = 'images/opened.gif';
$('img'+DemoID).title = '收缩';
$('span'+DemoID).className = 'opened';
}
};
TreeView.prototype.show = function(){
$(this.DemoID).innerHTML = this.HeaderStr.replace(/\{\$TreeName\}/g,this.TreeName) + this.xmlText + this.FooterStr;
};HTML代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Webshow_TreeViewTest</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
}
span
{
margin-left:18px;
padding-left:0;
}
li{list-style:none;}
.opened{display:block;}
.closed{display:none;}
-->
</style>
<script type="text/javascript" src="prototype.js" ></script>
<script type="text/javascript" src="TreeView.js" ></script>
<script type="text/javascript">
var TV = new TreeView();
TV.RequestUrl = "MenuList.xml";
TV.DemoID = "Demo";
TV.TreeName = "MenuList";
function load()
{
var pars = '';
var ajax = new Ajax.Request(TV.RequestUrl,{method: 'get',parameters: pars,onComplete: dataReceived});
}
function dataReceived(request){
TV.DataSource = request.responseXML.getElementsByTagName(TV.TreeName)[0].childNodes;
TV.DataBind(TV.DataSource);
TV.show();
}
window.onload = function(){try{load();}catch(e){alert(e);}};
</script>
</head>
<body style="font-size:12px;">
<div id="header"><h1>测试</h1></div>
<div id="Demo"></div>
<div id="footer"></div>
</body>
</html>
TREEVIEW.JS代码帖出/*under prototype.js*/
function TreeView(){
this.TreeName = "TreeView"; //TreeView名字
this.TreeID = "id" //树节点ID
this.TitleStr = "title" //节点标题
this.ContentStr = "content"; //节点内容
this.UrlStr = "url"; //节点超链接
this.RequestUrl = null; //数据路径
this.DemoID = null; //TreeView容器名
this.DataSource = null; //数据源
this.xmlText = ""; //数据内容
this.NodeID = ""; //节点ID
this.HeaderStr = "<div class='opened'><ul><li><img src='images/opened.gif' />{$TreeName}</li>";
this.ElementBeginStr = "<li><img id='img{$TreeID}' src='images/closed.gif' style='cursor:hand;' title='展开' onclick=\"javascript:Tree('{$TreeID}');\" />{$TitleStr}</li><span id='span{$TreeID}' class='closed'>";
this.ElementStr = "<li><img src='images/node.gif'><a href='{$UrlStr}' title='{$ContentStr}'>{$TitleStr}</a></li>";
this.ElementEndStr = "</span>";
this.FooterStr = "</ul></div>";
}
TreeView.prototype.DataBind = function(ds){
for(var i=0;i<ds.length;i++)
{
if(ds[i].hasChildNodes())
{
this.xmlText += this.ElementBeginStr.replace(/\{\$TreeID\}/g,ds[i].getAttribute(this.TreeID)).replace(/\{\$TitleStr\}/g,ds[i].getAttribute(this.TitleStr));
this.DataBind(ds[i].childNodes);
this.xmlText += this.ElementEndStr;
}
else
{
this.xmlText += this.ElementStr.replace(/\{\$UrlStr\}/g,ds[i].getAttribute(this.UrlStr)).replace(/\{\$ContentStr\}/g,ds[i].getAttribute(this.ContentStr)).replace(/\{\$TitleStr\}/g,ds[i].getAttribute(this.TitleStr));
}
}
};
Tree = function(DemoID){
if($('span'+DemoID).className == 'opened')
{
$('img'+DemoID).src = 'images/closed.gif';
$('img'+DemoID).title = '展开';
$('span'+DemoID).className = 'closed';
}
else
{
$('img'+DemoID).src = 'images/opened.gif';
$('img'+DemoID).title = '收缩';
$('span'+DemoID).className = 'opened';
}
};
TreeView.prototype.show = function(){
$(this.DemoID).innerHTML = this.HeaderStr.replace(/\{\$TreeName\}/g,this.TreeName) + this.xmlText + this.FooterStr;
};HTML代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Webshow_TreeViewTest</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
}
span
{
margin-left:18px;
padding-left:0;
}
li{list-style:none;}
.opened{display:block;}
.closed{display:none;}
-->
</style>
<script type="text/javascript" src="prototype.js" ></script>
<script type="text/javascript" src="TreeView.js" ></script>
<script type="text/javascript">
var TV = new TreeView();
TV.RequestUrl = "MenuList.xml";
TV.DemoID = "Demo";
TV.TreeName = "MenuList";
function load()
{
var pars = '';
var ajax = new Ajax.Request(TV.RequestUrl,{method: 'get',parameters: pars,onComplete: dataReceived});
}
function dataReceived(request){
TV.DataSource = request.responseXML.getElementsByTagName(TV.TreeName)[0].childNodes;
TV.DataBind(TV.DataSource);
TV.show();
}
window.onload = function(){try{load();}catch(e){alert(e);}};
</script>
</head>
<body style="font-size:12px;">
<div id="header"><h1>测试</h1></div>
<div id="Demo"></div>
<div id="footer"></div>
</body>
</html>
可能问题问的有点不清楚,我再请教下大家,我就是在XML中不会定义递归DTD字段导致不能通过ID来操作XML