How to insert table rows and columns with 2d array

后端 未结 2 1281
情深已故
情深已故 2021-01-28 02:08

SO I have 2 arrays which are var num = [\"1\", \"2\", \"3\"]; var cars = [\"Saab\", \"Volvo\", \"BMW\"];
and I made a button where it\'ll add rows and column and pl

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 02:29

    This is a simple solution using your approach.

    function myFunction() {
    
      var num = ["1", "2", "3"];
      var cars = ["Saab", "Volvo", "BMW"];
      var table = document.getElementById("myTable");
      var rows = new Array(cars.length);
      var numCell, carCell;
    
      for (var i = 0; i < cars.length; i++) {
        rows[i] = table.insertRow(i);
        numCell = rows[i].insertCell(0);
        carCell = rows[i].insertCell(1);
        numCell.appendChild(document.createTextNode(num[i]));
        carCell.appendChild(document.createTextNode(cars[i]));
      }
    
    }
    
    
    

提交回复
热议问题