【发布时间】:2012-11-28 00:58:04
【问题描述】:
我希望你能阅读我的代码,我的本地主机上有一个 XML 文件,我希望它从该文件中提取标题和年份(它目前有标题、年份、艺术家、价格和国家),同时维护页面上的 H1 和按钮。
H1 文本和按钮在单击时消失,我希望它与结果保持在同一页面上。
<h1>Show the Album list</h1>
<script>
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
function CdCatalog()
{
document.write("<table border='1'><th>TITLE</th><th>YEAR</th>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
}
</script>
【问题讨论】:
-
new XMLHttpRequest() -
不要使用
document.write,你应该使用document.body.innerHTML += "<td>blah</td>" -
谢谢 Derek,我还没学过,但我会看看一些教程。
-
你可以看看my demo。这并不难理解。
标签: javascript xml xmlhttprequest