【问题标题】:Switch between XML files using JavaScript使用 JavaScript 在 XML 文件之间切换
【发布时间】:2013-02-28 22:34:01
【问题描述】:

我使用 XML 文件作为我的 html 页面的布局并使用 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 样式表。

【问题讨论】:

    标签: php javascript html xml xmlhttprequest


    【解决方案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 append 函数来附加样式表)

    【讨论】: