JavaScript到jQuery(向表中添加行的函数)
有人能帮我把JavaScript转换成jQuery吗?JavaScript代码是:JavaScript到jQuery(向表中添加行的函数),javascript,jquery,Javascript,Jquery,有人能帮我把JavaScript转换成jQuery吗?JavaScript代码是: function addRow() { var table = document.getElementById("table2"); var numOfRows = table.rows.length; var numOfCols = table.rows[numOfRows-1].cells.length;
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";
}
}
函数addRow()
{
var table=document.getElementById(“表2”);
var numorrows=table.rows.length;
var numOfCols=table.rows[numOfRows-1].cells.length;
var newRow=table.insertRow(numorrows);
对于(var j=0;j
您可以尝试在最后一行之后添加新行的代码,我不知道您需要的shich参数
function addRow()
{
$('#table2 tr:last').after('<tr><td>some </td></tr><tr><td>text</td></tr>');
}
函数addRow()
{
$('表2 tr:last')。在('一些文本')之后;
}
试试这个
$('#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' />
$('#addRow')。单击(函数(){
var row=$(“#表2 tr:eq(0)”.clone();
$(行).find('td').html('add');
$('表2')。追加(行);
});
测试
为什么要将其转换为jQuery?重复?因为javascript代码应该使用jquery框架来完成…@apolo90,所以你得到否决票的原因是,当你提出确切的问题时,它会显示在推荐答案中。不要为了使用jquery而使用jquery。该库明显比本机javascript慢,尤其是在这样的小代码块上。这是chris的重复链接吗?非常感谢,但不起作用:s返回一些javascript错误?好的。。它在工作。。。谢谢:)好的,太好了!不客气