简体   繁体   English

如何使用JavaScript将行和单元格附加到HTML表

[英]How to append rows and cell to an HTML table with JavaScript

I need help trying to dynamically create an HTML table using JavaScript. 我需要帮助来尝试使用JavaScript动态创建HTML表。

Here is the html: 这是html:

<table id="Table">
    <tr>
        <th>Record:</th>
        <th>Employee Name:</th>
        <th>Amount:</th>
    </tr>
</table>

I now want to populate that table with an array dynamically using JavaScript. 我现在想使用JavaScript使用数组动态填充该表。
So far, here is what I have: 到目前为止,这是我所拥有的:

for(var i=0;i<info.length;i++){
    var tr=document.createElement('tr');
    for(var j=0;j<1;j++){
        var td=document.createElement('td');
        td.appendChild(document.createTextNode(i));
        tr.appendChild(td); 

        td=document.createElement('td');
        td.appendChild(document.createTextNode(info[i]));
        tr.appendChild(td);

        td=document.createElement('td');
        td.appendChild(document.createTextNode(hours[i]));
        tr.appendChild(td);
    }
    tbdy.appendChild(tr);
}
tbl.appendChild(tbdy);
body.appendChild(tbl);

It is not working... Any help? 它不起作用...有帮助吗?

I was able to accomplish this by using @ZMF's link 我能够通过使用@ZMF的链接来完成此操作

            table = document.getElementById('Table') 

            for(var i=0;i<info.length-1;i++){
                var record = i+1;
                var row = table.insertRow(1);
                var cell0 = row.insertCell(0);
                var cell1 = row.insertCell(1);
                var cell2 = row.insertCell(2);
                cell0.innerHTML = record;
                cell1.innerHTML = info[i];
                cell2.innerHTML = hours[i];

            }
        }

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

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