问题:
尝试从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>
答案 0 :(得分:1)
它应该是这样的:
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>");