【问题标题】:Drawing a table with vanilla JS and loops用香草 JS 和循环绘制表格
【发布时间】:2016-02-25 12:09:51
【问题描述】:

我正在做一个练习(从开始 Javascript)来更好地理解 DOM 操作。尝试仅使用 JS 在 DRY 方法中重新创建下表(教科书的解决方案是here):

<table>
    <tr>
        <td>Car</td>
        <td>Top Speed</td>
        <td>Price</td>
    </tr>
    <tr>
        <td>Chevrolet</td>
        <td>120mph</td>
        <td>$10,000</td>
   </tr>
   <tr>
       <td>Pontiac</td>
       <td>140mph</td>
       <td>$20,000</td>
   </tr>
</table>

我试过了,但不确定如何在不引发错误的情况下循环创建变量:

var array = [['Car', 'Top Speed', 'Price'],['Chevrolet', '120mph', '$10,000'], ['Pontiac', '140pmh', '$20,000']] // Creating a data array which a loop will source from

    var table = document.createElement('table');
    document.body.appendChild(table); // Drew the main table node on the document

    for (var i = 0; i<3; i++) { 
        var tr[i] = document.createElement('tr'); //Create 3 <tr> elements assigned to a unique variable BUT need a working alternative for 'tr[i]'
        table.appendChild(tr[i]); // Append to <table> node

        for (var j = 0; j<3; j++) {

            var tdText = document.createTextNode(array[i][j]); // Extract data from array to a placeholder variable
            tr[i].appendChild(tdText); // Take string from placeholder variable and append it to <tr> node
        }
    }

【问题讨论】:

标签: javascript html dom


【解决方案1】:

请使用tr 而不是tr[i]。它会工作

var array = [['Car', 'Top Speed', 'Price'],['Chevrolet', '120mph', '$10,000'], ['Pontiac', '140pmh', '$20,000']] // Creating a data array which a loop will source from

    var table = document.createElement('table');
    document.body.appendChild(table); // Drew the main table node on the document

    for (var i = 0; i<3; i++) { 
        var tr = document.createElement('tr'); //Create 3 <tr> elements assigned to a unique variable BUT need a working alternative for 'tr[i]'
        table.appendChild(tr); // Append to <table> node

        for (var j = 0; j<3; j++) {
            var tdElement = document.createElement('td');
            tdElement.innerHTML = array[i][j];
            tr.appendChild(tdElement); // Take string from placeholder variable and append it to <tr> node
        }
    }

【讨论】:

    【解决方案2】:

    如前所述,问题在于声明 tr[i] 变量时的语法错误。

    更简洁的方法是使用 table api 方法,例如

    var array = [
        ['Car', 'Top Speed', 'Price'],
        ['Chevrolet', '120mph', '$10,000'],
        ['Pontiac', '140pmh', '$20,000']
      ] // Creating a data array which a loop will source from
    
    var table = document.createElement('table');
    document.body.appendChild(table); // Drew the main table node on the document
    
    array.forEach(function(row) {
      var tr = table.insertRow(); //Create a new row
    
      row.forEach(function(column) {
        var td = tr.insertCell();
        td.innerText = column; // Take string from placeholder variable and append it to <tr> node
      });
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多