2012-11-22 52 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' />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​