Javascript将数据插入HTML表格

时间:2010-12-16 05:06:09

标签: javascript html

这是一个相当简单的问题,我在将数据从Javascript插入HTML表时遇到问题。

以下是我的JavaScript摘录:

更新 - 我摆脱了两个循环并将其简化为一个,但仍然存在问题..

    for (index = 0; index < enteredStrings.length; index++) {
        output.innerHTML += "<td>" + enteredStrings[index] + "</td>"
        + "<td>" + enteredStringsTwo[index] + "</td>";
        nameCounter++;
        total.innerHTML = "Total: " + nameCounter;
    }

以下是我的HTML页面之外的内容:

<table id="nameTable">
   <tr>
     <th>First</th><th>Last</th>
   </tr>

更新图片:

alt text

4 个答案:

答案 0 :(得分:6)

试试这个(已编辑):

var tableContent = '<tr>';
for (index = 0; index < enteredStrings.length; index++) {
    tableContent += "<td>" + enteredStrings[index] + "</td>";
    nameCounter++;  // I don't know if this should be there, 
                    // logically the counter should be incremented here as well?
    total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr><tr>';

for (index = 0; index < enteredStringsTwo.length; index++) {
    tableContent += "<td>" + enteredStringsTwo[index] + "</td>";
    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr>';
output.innerHTML += tableContent;

Edit2(更新的问题代码):

var tableContent = '<tr>';
for (index = 0; index < enteredStrings.length; index++) {
    tableContent += "<td>" + enteredStrings[index] + "</td>"
    + "<td>" + enteredStringsTwo[index] + "</td>";
    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr>';
output.innerHTML += tableContent;

Edit3(查看电子邮件中发送的代码后):

var tableContent = "";

for (index = 0; index < enteredStrings.length; index++) {
    tableContent += "<tr><td>" + enteredStrings[index] + "</td>"

    + "<td>" + enteredStringsTwo[index] + "</td></tr>";

    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}
output.innerHTML = tableContent;

答案 1 :(得分:3)

而不是关闭你打开新的td 尝试

    for (index = 0; index < enteredStrings.length; index++) {
    output.innerHTML += "<td>" + enteredStrings[index] + "</td>";
    total.innerHTML = "Total: " + nameCounter;
}

for (index = 0; index < enteredStringsTwo.length; index++) {
    output.innerHTML += "<td>" + enteredStringsTwo[index] + "</td>";
    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}

更新: 您附加 表格而不是。 在这种情况下,浏览器会在追加每个td后自动为您创建一行。

答案 2 :(得分:0)

稍微修改一下代码,

    var outputTbl = document.getElementById('nameTable');

    var output = document.createElement("tr");
    outputTbl.appendChild(output);

    for (index = 0; index < enteredStrings.length; index++) {
        output.innerHTML += "<td>" + enteredStrings[index] + "</td>";
        total.innerHTML = "Total: " + nameCounter;
    }

    for (index = 0; index < enteredStringsTwo.length; index++) {
       output.innerHTML += "<td>" + enteredStringsTwo[index] + "</td>";
       nameCounter++;
       total.innerHTML = "Total: " + nameCounter;
    }

答案 3 :(得分:0)

如果你需要在这里添加内部html代码。

<table id="nameTable" style="width:300px;">
    <tr>
        <th>First</th><th>Last</th>
    </tr>
</table>

在这种情况下,您可以使用Jnerator

如果这是您的数据:

var data = [
    { first: 'Cole', last: 'Alan'},
    { first: 'Michael', last: 'Scott'}
]

您可以通过下一种方式将它们添加到您的表格中:

for(var i=0; i<data.length; i++) {
    var item = data[i];
    var row = $j.tr({ child:[$j.td(item.first), $j.td(item.last)] });
    nameTable.appendChild(row.dom());
}
nameTotal.innerHTML = 'Total: ' + data.length;

This is example