2012-11-22 55 views
-2

任何人都可以幫助我將JavaScript轉換爲jQuery嗎? JavaScript代碼是:JavaScript to jQuery(函數添加行到表)

function addRow() 
{  
    var table = document.getElementById("table2");  
    var numOfRows = table.rows.length;  
    var numOfCols = table.rows[numOfRows-1].cells.length;      
    var newRow = table.insertRow(numOfRows); 

    for (var j = 0; j < numOfCols; j++) {      
      newCell = newRow.insertCell(j);      
      newCell.innerHTML = "add"; 
    } 
} 
+4

你爲什麼要把它轉換成jQuery? – Cerbrus

+2

重複? http://stackoverflow.com/questions/171027/add-table-row-in-jquery – chris

+0

因爲javascript代碼應該使用jquery框架來完成... – apolo90

回答

0

你可以試試這個代碼,在最後添加一個新行,我don0t知道你需要

function addRow() 
{  
    $('#table2 tr:last').after('<tr><td>some </td></tr><tr><td>text</td></tr>'); 
} 
+0

這是從意大利麪克里斯'重複的鏈接? – st3inn

+0

非常感謝,但不起作用:S – apolo90

+0

返回javascript的一些錯誤? –

1

shich參數試試這個

DEMO

$('#addRow').click(function(){ 
    var row = $('#table2 tr:eq(0)').clone(); 
    $(row).find('td').html('add'); 
    $('#table2').append(row); 
}); 

<table id='table2'> 
    <tr><td>test</td></tr> 
</table> 
​<input type='button' id='addRow' value='click' />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​