[英]Outputting XML data to HTML by use of JS
問題:
嘗試一次從XML文件中打印一個問題和4個答案。
JS代碼:
var xmlDoc, quest, ans, i, n;
xmlDoc = loadXMLDoc("questions.xml");
quest = xmlDoc.getElementsByTagName('main');
document.write("<table border='1'>");
for (i = 0; i < quest.length; i+=1)
{
document.write("<tr><td>");
document.write( quest[i].childNodes[0].nodeValue );
document.write("</td></tr>");
for(n = 0; n < 4; n++)
{
document.write("<tr><td>");
document.write( quest[i].childNodes[n].nodeValue );
document.write("</td></tr>");
}
}
document.write("</table>");
所需的輸出:
每個問題下面都有四個答案。 現在,只有問題可以正確打印。
XML文件的結構為:
<main>
<instruction></instruction>
<solution></solution>
<solution></solution>
<solution></solution>
<solution></solution>
</main>
應該是這樣的:
var xmlDoc, quest, ans, i, n;
xmlDoc = loadXMLDoc("questions.xml");
quest = xmlDoc.getElementsByTagName('main');
document.write("<table border='1'>");
for (i = 0; i < quest.length; i+=1)
{
document.write("<tr><td>");
document.write( quest[i].childNodes[0].nodeValue );
document.write("</td></tr>");
for(n = 1; n < 5; n++) // m = 1 because [0] is the title.
{
document.write("<tr><td>");
document.write( quest[i].childNodes[n].nodeValue );
document.write("</td></tr>");
}
}
document.write("</table>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.