简体   繁体   English

使用JS将XML显示为HTML中的表格仅显示一行

[英]Using JS to display XML as a table in HTML only shows one row

The code below only shows the first row of the table, it does not show the remaining elements of the XML file. 下面的代码仅显示表的第一行,而不显示XML文件的其余元素。 I have no idea why. 我不知道为什么。

Edit: I see part of the problem is that vegTable is a variable. 编辑:我看到部分问题是vegTable是一个变量。 I need a function in the for loop. 我需要在for循环中使用一个函数。

Edit 2: I can now increment the table to get the proper amount of rows, but the first row of data is all that displays, multiple times. 编辑2:现在,我可以增加表以获取适当数量的行,但是第一行数据多次显示。

JSFiddle JSFiddle

function getVeggies() {
"use strict";
var veg, sci, vegTable;
veg = (cc[dd].getElementsByTagName("veg")[0].childNodes[0].nodeValue);
sci = (cc[dd].getElementsByTagName("sciname")[0].childNodes[0].nodeValue);

for (dd = 0; dd < cc.length; dd++) {

vegTable += "<tr><td>" + veg + "</td><td>" + sci + "</td></tr>";
}

document.getElementById('fullTable').innerHTML = vegTable;
dd = 0;
}

Try moving the element getting logic into the loop: 尝试将获取逻辑的元素移入循环:

function getVeggies() {
    "use strict";
    var veg, sci, vegTable;
    var cc = cc;//whats cc?

    for (var i = 0; i < cc.length; i++) { 
        veg = (cc[i].getElementsByTagName("veg")[0].childNodes[0].nodeValue);
        sci = (cc[i].getElementsByTagName("sciname")[0].childNodes[0].nodeValue);

        vegTable += "<tr><td>" + veg + "</td><td>" + sci + "</td></tr>";
    }

    document.getElementById('fullTable').innerHTML = vegTable;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM