0

こんにちは、次のようなコードがあります。

function addRowToTable(num){
if (hasLoaded) {
    var tbl = document.getElementById(TABLE_NAME);
    var nextRow = tbl.tBodies[0].rows.length;
    var iteration = nextRow + ROW_BASE;
    if (num == null) {
        num = nextRow;
    } else {
        iteration = num + ROW_BASE;
    }

    // add the row
    var row = tbl.tBodies[0].insertRow(num);

    // cell
    var cell0 = row.insertCell(0);
    var LNInpT = document.createElement('input');
    LNInpT.setAttribute('type', 'text');
            LNInpT.setAttribute('value', 'name');
            cell0.appendChild(LNInpT);

    // Pass in the elements you want to reference later
    // Store the myRow object in each row
    row.myRow = new myRowObject(textNode, txtInp, cbEl, raEl);
}}

このコードを編集して、テーブルの最初の行の前に新しい行を追加するのを手伝ってください。タンク

4

3 に答える 3

1

これはあなたを助けるはずです:

var myTable = document.getElementById('myTable');
var tbody = myTable.tbodies[0];
var tr = tbody.insertRow(-1); // puts it at the start

var td = document.createElement('td');
td.innerHTML = "Something";
tr.appendChild(td);
于 2012-07-19T15:00:08.813 に答える
0

これをはるかに簡単にするjQueryを使用することをお勧めします(prependメソッドを使用)。できない、またはしたくない場合は、insertBeforeを使用できるはずです。

SOでこの質問を見たいと思うかもしれません。

于 2012-07-19T13:50:11.543 に答える