簡體   English   中英

使用JavaScript動態創建表格

[英]Dynamic table creation using javascript

我正在使用類似的東西來創建動態表

    for(var i=0;i<nrRows;i++){
        row=document.createElement('tr');
        for(var j=0;j<nrCols;j++){
        cell=document.createElement('td');
        cell.appendChild(document.createTextNode(i+' '+j))
        row.appendChild(cell);
        }
        tbo.appendChild(row);
    }

現在,我希望第一行填充列標題。 我想為每行中的每個文本框提供ID。 我該怎么做?

“現在,我希望第一行填充列標題。”

cell = document.createElement(i===0 ? 'th' : 'td');

“而且我想為每行中的每個文本框提供ID。”

什么文本框? 您當前未創建文本框。 每個id屬性應該是唯一的,因此,假設您實際上已經創建了一些文本框,則可以將id設置為i + "_" + j (類似於您為.createTextNode()獲得的.createTextNode() ),以便您的代碼可以輕松計算訪問任何特定單元格所需的id

if(i == 0)
{
    cell = document.createElement('th');
}
else
{

    cell = document.createElement('td');
    var inputFld = document.createElement('input');
    inputFld.type = "text"; //input type = textfield
    var count=table.rows.length;
    inputFld.id = "NewTb" + count;
    cell.appendChild(inputFld);
}
if(i == 0)
{
    cell = document.createElement('th');
    //give column header names
}
else
{
    cell = document.createElement('td');
    var inputFld = document.createElement('input');
    inputFld.type = "text"; //input type = textfield
    inputFld.id = "input" + i;//assign id to input field
    cell.appendChild(inputFld);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM