在java脚本的第一行之前添加新行

Add new Row before first row in java script

本文关键字:一行 新行 添加 脚本 java      更新时间:2023-12-04

嗨,我有如下代码:

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);
}}

请帮助我在编辑此代码时在表的第一行之前添加新行。储罐

这应该会对您有所帮助:

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);

我建议您使用jQuery,这将使这更容易(使用prepend方法)。如果你不能或不想,你应该能够使用insertBefore。

您可能想在SO上看看这个问题。

使用jQuery的prepend()方法。