Dynamically generated table - using an array to fill in TD values

前端 未结 1 1439
长情又很酷
长情又很酷 2020-12-09 17:36

I need your help,

For some reason, I can\'t get the data captured in my array to populate into the TD cells of my dynamically generated table:



        
相关标签:
1条回答
  • 2020-12-09 18:15

    Change:

    var tr = document.createElement('TR'); // this should be inside the first loop
    tableBody.appendChild(tr); // this should be just before the first loop is over
    
    for (i=0; i<stock.length; i++) {
        for (j=0; j<stock[i].length; j++)    {
          var td = document.createElement('TD')
          td.appendChild(document.createTextNode(stock[i][j]));
          td.appendChild(td) // this should be tr.appendChild(td)
        }
    }
    

    to this:

    for (i = 0; i < stock.length; i++) {
        var tr = document.createElement('TR');
        for (j = 0; j < stock[i].length; j++) {
            var td = document.createElement('TD')
            td.appendChild(document.createTextNode(stock[i][j]));
            tr.appendChild(td)
        }
        tableBody.appendChild(tr);
    }
    

    Fiddle

    0 讨论(0)
提交回复
热议问题