2012-07-19 29 views
0

嗨第一行之前添加新行我的代碼如下:在Java腳本

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

請幫我在表格第一行之前添加新行,編輯該代碼。 坦克

+0

你有沒有考慮使用$( '#myTable的TR:第一')。前( ' ...');? – 2012-07-19 14:02:06

回答

0

我建議你使用jQuery,這將使這更容易(與prepend方法)。如果你不能或不想,你應該可以使用insertBefore

你可能想看看這個question on SO

+0

什麼不工作,你想達到什麼目的? – Peter 2012-07-19 14:44:07

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

根據insertRow函數的文檔。您應該使用0而不是-1來將行添加爲表的第一行。 - https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement.insertRow – MicTech 2013-11-20 20:53:22