2012-07-19 7 views
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); 
}} 

날이 코드를 편집하여 테이블의 첫 번째 행하기 전에 새 행을 추가하는 데 도움이 바랍니다. 탱크

+0

당신이 ('#의 myTable에의 TR : 첫 번째') $를 사용하는 것으로 간주 되세요 (' ...') 전에;? –

답변

0

prepend 메서드를 사용하면이 작업을 훨씬 쉽게 수행 할 수있는 jQuery를 사용하는 것이 좋습니다. 할 수 없거나 원하지 않는 경우 insertBefore을 사용할 수 있어야합니다.

question on SO을 볼 수 있습니다.

+0

무엇이 효과가없고 무엇을 성취하려고합니까? – Peter

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 함수에 대한 문서에 따릅니다. 행을 테이블의 첫 번째 행으로 추가하려면 -1 대신 0을 사용해야합니다. - https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement.insertRow – MicTech

관련 문제