最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create, table cell with innerHTML valuetextnode via loop of array - Stack Overflow

programmeradmin0浏览0评论

I have tried to run a Javascript loop to create a table with values in it from an array: I appended the text-node and tried that the text node will be the array values, one per td for the length. This is my code so far, it will only create one cell with "undefined" value:

function addtd(){
      var table1 = document.getElementsByTagName('table')[0];
      var rowrow =  document.createElement('tr');
      var foods = new Array();

      foods[0] = "milk" ;`enter code here`
      foods[1] = "meat" ;
      foods[2] = "fruit" ;
      foods[3] = "fish" ;
      foods[4] = "salad" ;

      for ( var i=0;i<foods.length;i++)

      var cell1  =  document.createElement('td').innerHTML = 'foods[i]';    
      var text1 = document.createTextNode(foods[i]) ;

      cell1.appendChild(text1);
      rowrow.appendChild(cell1);
      table1.appendChild(rowrow);
      }

       <div id="divfood"> </div>
       <button onclick="addtd()">Click me</button>`enter code here`
       <table border="2" id="tabletable" cellspacing="5" >
       </table>   

I have tried to run a Javascript loop to create a table with values in it from an array: I appended the text-node and tried that the text node will be the array values, one per td for the length. This is my code so far, it will only create one cell with "undefined" value:

function addtd(){
      var table1 = document.getElementsByTagName('table')[0];
      var rowrow =  document.createElement('tr');
      var foods = new Array();

      foods[0] = "milk" ;`enter code here`
      foods[1] = "meat" ;
      foods[2] = "fruit" ;
      foods[3] = "fish" ;
      foods[4] = "salad" ;

      for ( var i=0;i<foods.length;i++)

      var cell1  =  document.createElement('td').innerHTML = 'foods[i]';    
      var text1 = document.createTextNode(foods[i]) ;

      cell1.appendChild(text1);
      rowrow.appendChild(cell1);
      table1.appendChild(rowrow);
      }

       <div id="divfood"> </div>
       <button onclick="addtd()">Click me</button>`enter code here`
       <table border="2" id="tabletable" cellspacing="5" >
       </table>   
Share Improve this question edited May 27, 2021 at 16:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 27, 2012 at 21:26 GambrinusGambrinus 411 gold badge1 silver badge2 bronze badges 1
  • might be of some help: evontech./login/topic/1079.html – Rubens Commented Dec 27, 2012 at 21:28
Add a ment  | 

3 Answers 3

Reset to default 2

I would change this -

var cell1  =  document.createElement('td').innerHTML = 'foods[i]'; 

to

var cell1  =  document.createElement('td');
cell1.innerHTML = 'foods[i]'; 

I wouldn't expect that the cell1 will get set correctly when you are chaining its innerHTML inline to cell1, but haven't tested it.

Something like this will loop through your array and create a row of it, as that seems to be what you are trying to do.

var foods = ["milk", "eggs", "lettuce", "tomato"],
    food,
    row = document.createElement("tr");

while (food = foods.shift()) {
    var el = document.createElement("td");
        el.innerText = food;

    row.appendChild( el );
}

document.getElementsByTagName("table")[0].appendChild(row);

You have several errors in your code including missing braces around the for loop and weird looking 'enter code here' sentence on the 6th line. You also forgot to put your JavaScript code inside the <script> tag. Here is the working version of your code:

<script>
function addtd(){
    var table1 = document.getElementsByTagName('table')[0];
    var rowrow =  document.createElement('tr');
    var foods = new Array();

    foods[0] = "milk" ;
    foods[1] = "meat" ;
    foods[2] = "fruit" ;
    foods[3] = "fish" ;
    foods[4] = "salad" ;

    for ( i=0; i <foods.length; i++) {
        var cell1  =  document.createElement('td');
        var text1 = document.createTextNode(foods[i]);
        cell1.appendChild(text1);
        rowrow.appendChild(cell1);
    }
    table1.appendChild(rowrow);
}
</script>

<div id="divfood"> </div>
<button onclick="addtd()">Click me</button>
<table border="2" id="tabletable" cellspacing="5" >
</table>   ​

Here is the jsFiddle demo.

发布评论

评论列表(0)

  1. 暂无评论