2013-02-28 82 views
0

我使用XML文件作为我的html页面的布局,并使用JavaScript加载它们 这样的:XML文件之间切换使用JavaScript

if (window.XMLHttpRequest) 
    { 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

xmlhttp.open("GET","default.xml",false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseXML; 

我如何2点或更多的布局之间切换?

我使用javascript/XML将XML加载到HTML这样的:

document.write('<ul id="horizontal-list">'); 
var x=xmlDoc.getElementsByTagName("APP"); 
for (i=0;i<x.length;i++) 
    { 
    document.write('<li><a class="app_link" href="depiction.php?app='); 
    document.write(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue); 
    document.write('&dl='); 
    document.write(x[i].getElementsByTagName("DOWNLOAD")[0].childNodes[0].nodeValue); 
    document.write('&install='); 
    document.write(x[i].getElementsByTagName("INSTALL")[0].childNodes[0].nodeValue); 
    document.write('">'); 
    document.write('<label class="app_label">'); 
    document.write(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue); 
    document.write('</label><img class="applicationIcon" src="'); 
    document.write(x[i].getElementsByTagName("ICON")[0].childNodes[0].nodeValue); 
    document.write('"/></a></li>'); 
    } 
document.write('</ul>'); 

注:我使用一个CSS样式表也。

回答

1

创建Ajax功能像下面并调用与路径的功能,以您的XML文件:

function getXml($file){ 
if (window.XMLHttpRequest) 
    { 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

xmlhttp.open("GET",$file,false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseXML; 
} 

,你也可以发送你的CSS文件太(添加第二个参数的功能和使用jquery追加函数来追加样式表)

+0

非常感谢! @MIIB – atomikpanda 2013-02-28 22:53:27