使用JS在HTML中将XML显示为表只显示一行

时间:2021-03-15 16:53:25

The code below only shows the first row of the table, it does not show the remaining elements of the XML file. I have no idea why.

下面的代码仅显示表的第一行,它不显示XML文件的其余元素。我不知道为什么。

Edit: I see part of the problem is that vegTable is a variable. I need a function in the for loop.

编辑:我看到部分问题是vegTable是一个变量。我需要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

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;
}

1 个解决方案

#1


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;
}

#1


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;
}