2013-02-28 80 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