<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax 无限树 by 快乐笛子(
[email protected])</title>
<style type="text/css">
*{ padding:0; margin:0}
body { font:12px "宋体"}
.tree { border:1px solid #ccc; margin:30px; width:200px; height:450px; float:left; padding:12px 12px 12px 0px}
.tree ul { margin-left:12px}
.tree li { list-style-type:none; margin:10px 0px; }
.tree li span { color:#FF0000; margin-left:4px}
.tree li a.close{ background:url(close.gif) no-repeat 1px 1px; width:11px; height:11px; font-size:1px; margin-right:4px; clear:left}
.tree li a.open{ background:url(open.gif) no-repeat 1px 1px; width:11px; height:11px; font-size:1px; margin-right:4px;clear:left}
.tree li a.nonedata{ background:url(nonedata.gif) no-repeat 1px 1px; width:11px; height:11px; font-size:1px; margin-right:4px;clear:left}
.tree li a { text-decoration:none; color:#000000; float:left}
textarea{ font-size:11px; font-family:Tahoma; width:300px; height:400px; line-height:18px; margin:30px; padding:2px}
</style>
<script type="text/javascript">
var xmlhttp;
//创建xmlhttp实例
function createxmlhttprequest(){
try {
xmlhttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlhttp = false;
}
}
}
if (!xmlhttp)
alert("创建 Ajax 实例出错!");
}
function delloadingtext(){
if(innerPlace.tagName!="DIV"){
innerPlace.getElementsByTagName("a")[1].innerHTML = innerPlace.getElementsByTagName("a")[1].innerHTML.substr(0,(innerPlace.getElementsByTagName("a")[1].innerHTML.length-loadingtext.length));
}
}
var onsuccess = "";
var onloading = "";
var onerror = "";
var loadingtext = "<span>Loading...</span>"
var AjaxRequestObj = "";
//封装 ajax 类
function Ajax(method,url,asynchronous,onsuccessFun,onloadingFun,onerrorFun){
onsuccess = onsuccessFun;
onloading = onloadingFun;
onerror = onerrorFun
this.asynchronous = true;
this.onloading=function(){onloadFun};
this.onerror=function(){onerrorFun};
createxmlhttprequest();
xmlhttp.onreadystatechange = handleStateChange;
xmlhttp.open(method,url,asynchronous);
this.request = function(){xmlhttp.send(null);}
}
function handleStateChange(){
if (xmlhttp.readyState==4){
if(xmlhttp.status==200){
AjaxRequestObj = xmlhttp.responseXML;
eval(onsuccess);
}else{
if(xmlhttp.status==404)
eval(onerror); //页面不存在
}
}else{
if (xmlhttp.readyState==3)
eval(onloading);
}
}
var innerPlace="";
function innerloadingtext(obj){
if(obj.parentNode.getElementsByTagName("a")[0].className=="open"){
try{
ulobj = obj.parentNode.getElementsByTagName("ul")[0];
}catch(e){ulobj = "";}
if(typeof(ulobj)!="object")
obj.innerHTML += loadingtext;
}
}
function switchclass(id,obj){ //改变li的样式,从样式判断是否需要读取数据
if(obj.className=="close"){
obj.className="open";
try{
ulobj = obj.parentNode.getElementsByTagName("ul")[0];
}catch(e){ulobj = "";}
if(typeof(ulobj)=="object"){
if(ulobj.style.display="none"){
ulobj.style.display="block";
}else{
getValue(id,obj.parentNode);
}
}else{
getValue(id,obj.parentNode);
}
}else{
if(obj.className=="open"){obj.className="close";obj.parentNode.getElementsByTagName("ul")[0].style.display="none";}
}
}
function getValue(id,obj){ //取值
var ajaxObj = new Ajax("GET","list.asp?id="+id,true,"getsuccess()","getting()","getfailed()");
ajaxObj.request();
innerPlace = obj;
}
function getsuccess(){ //取值成功后
var addr = AjaxRequestObj.getElementsByTagName("address");
var addrLen = addr.length;
if(addrLen==0){
setTimeout("delloadingtext()",200)
innerPlace.firstChild.className="nonedata";
}else{
var subnode="";
var selfid;
var name;
var url;
for (i=0; i<addrLen; i++){
selfid = addr[i].firstChild.data.split("|")[0];
name = addr[i].firstChild.data.split("|")[1];
url = addr[i].firstChild.data.split("|")[2];
if(url!="")
subnode = subnode + "<li><a href='javascript:void(0)' onclick="switchclass("+ selfid +",this);innerloadingtext(this.parentNode.getElementsByTagName('a')[1])" class='close'> </a><a href='"+ url +"' target='mainFrame' onclick="switchclass("+ selfid +",this.parentNode.getElementsByTagName('a')[0]);innerloadingtext(this)">" + name + "</a></li>";
else
subnode = subnode + "<li><a href='javascript:void(0)' onclick="switchclass("+ selfid +",this);innerloadingtext(this.parentNode.getElementsByTagName('a')[1])" class='close'> </a><a href='javascript:void(0)' onclick="switchclass("+ selfid +",this.parentNode.getElementsByTagName('a')[0]);innerloadingtext(this)">" + name + "</a></li>";
}
subnode = "<ul>" + subnode + "</ul>";
setTimeout("delloadingtext()",200)
innerPlace.innerHTML = innerPlace.innerHTML + subnode;
document.getElementById("stru").value = document.getElementById("tree").innerHTML
}
}
function getting(){ //正在取值时
// innerPlace.innerHTML = "Loading...";
}
function getfailed(){ //取值失败
alert("err")
}
</script>
</head>
<body onload="getValue(0,document.getElementById('tree'))">
<h1 style=" margin:20px">Ajax无限级目录树:</h1>
<div class="tree" id="tree"></div>
<iframe src="" name="mainFrame" id="mainFrame" frameborder="1" style=" border:none; margin:30px; width:250px; height:400px; padding:0"></iframe>
<textarea id="stru"></textarea>
<div style="clear:both; text-align:center">Writen by misshjn (@ <a href="http://www.happyshow.org" target="_blank">http://www.happyshow.org</a>)</div>
</body>
</html>
演示示例是一个通过目录树控制iframe的例子。示例的右侧是目录树的文档结构代码,每点击一次节点,此代码都会实时变化,可以把这些代码存入cookie,这样就可以保留目录树的状态而不怕页面被刷新了。当然body标签的onload得变一变才行。但目录树的结构代码字符量比较多,这个cookie一定比较大,so~,呵呵,还是没有把保留状态的功能做入示例中。