使用 JS 将 XML 数据输出到 HTML
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>");
相关文章:
- 使用Jade输出HTML(在ng个重复内)
- 输出 html 表单数据
- 如何通过 AngularJS 模板输出 html
- Ajax 输出 html 注释
- 显示查询输出 HTML 中的图像
- HTML 代码不适用于 jquery 调用,而是输出 HTML 代码
- Html.ActionLink onclick JavaScript 函数未被调用,并且函数名称在输出 HTML 中已损
- Javascript -使用字符串输出HTML
- 如何正确使用jQuery从php输出html字符串
- 为什么.html()不输出html标签
- 如何获得一个变量's输出html值
- Twitter-text.js输出html作为纯文本
- 多行文本文件输出html
- 如何使用XSLT匹配元素和输出HTML
- 输出HTML代码
- 输出HTML时出现意外的令牌错误
- 用JavaScript输出HTML实体
- 在rjerb模板中输出HTML
- 从angularjs过滤器输出HTML,无需ng绑定HTML
- 获取实际输出HTML的函数