分配Id并在Javascript中遍历行

时间:2015-10-06 22:26:14

标签: javascript html for-loop html-table getelementbyid

我创建了一个表,看起来工作得很好但是我在向这个表分配一个id时会遇到问题,计算它有多少行,并为每一行分配一个id。调试器说:

  

TypeError:document.getElementById(...)为null

我无法弄清楚我做错了什么。有人可以帮忙吗?我也在下面的代码中评论了我的问题:

function populateTable(list){
    var tableContent = "<table>";   
    for (var i = 0; i < list.length; ++i) {
          var record = list[i];
        tableContent += "<tr><td>" + record.Title + "</td></tr>\n";
    }   
tableContent += "</table>";
tableContent.id="orders";
var rows = document.getElementById("orders").rows.length;//why is this null? 
document.getElementById("test").innerHTML = rows;   
   for (var i=0; i< rows; ++i){
           //how do I assign an id for the element here? 
   }
}

3 个答案:

答案 0 :(得分:3)

您可以这样做:

<强> HTML

<div id="here"> </div> <!-- the table will be added in this div -->

<强>的JavaScript

function populateTable(list){
    var tableContent = document.createElement('table');
    for (var i = 0; i < list.length; ++i) {
        var record = list[i];
        var cell = document.createElement('td');
        var row = document.createElement('tr');
        var textnode = document.createTextNode(record.Title);
        cell.appendChild(textnode);
        row.appendChild(cell);
        tableContent.appendChild(row);
    }   
    tableContent.id="orders";
    document.getElementById("here").appendChild(tableContent);  // the table is added to the HTML div element
    var rows = document.getElementById("orders").rows;
    for (var i=0; i < rows.length; ++i){
        rows[i].id = "myId" + (i+1);  // this is how you assign IDs
        console.log(rows[i]);
    }

}

var persons = [{Title:"John"}, {Title:"Marry"}];
populateTable(persons);

答案 1 :(得分:0)

修改

您似乎不知道如何从javascript正确创建DOM:

http://www.w3schools.com/jsref/met_document_createelement.asp

旧答案

这一行:

var rows = document.getElementById("orders").rows.length;//why is this null?

通过id从HTML文档中获取元素。看起来您还没有在文档中添加tableContent

答案 2 :(得分:0)

这是我的建议(阅读代码中的评论):

// This will create a table element and return it

function createTable(list){
    var table = document.createElement('table'); // This is an actual html element
    table.id = 'orders'; // Since it's an html element, we can assign an id to it

    tableHtml = ''; // This empty string will hold the inner html of the table we just created
    for (var i = 0; i < list.length; ++i) {
        var record = list[i];

        // we can assign the id here instead of looping through the rows again
        tableHtml += '<tr id="row-' + i + '"><td>' + record.Title + '</td></tr>';
    }   
    table.innerHTML = tableHtml; // We insert the html into the table element and parse it

    var rows = table.rows.length; // We already have a reference to the table, so no need of getElementById

    alert('rows'); // rows holds the number of rows. You can do whatever you want with this var.

    return table; // return the table. We still need to insert it into the dom
}


// Create a new table and hold it in memory
var myTable = createTable([{Title:'One'}, {Title:'Two'}, {Title:'Three'}]);

// Inset the newly created table into the DOM
document.getElementById('parent-of-table').appendChild(myTable);

希望这有帮助

这已经得到了解答,但这是我的版本没有所有评论:

function createTable(list){
    var table = document.createElement('table');
    table.id = 'orders';
    tableHtml = '';
    for (var i = 0; i < list.length; ++i) {
        tableHtml += '<tr id="row-' + i + '"><td>' + list[i].Title + '</td></tr>';
    }   
    table.innerHTML = tableHtml;
    var rows = table.rows.length;
    alert('rows');
    return table;
}