append row after first row in a html table

I have var row=<tr><td>val</td><td>val2</td></tr> and I tried this:

$("#mainTable tbody").append(row);

but it appends to the end of the table.

Also I tried $("#mainTable tr:first").after().append(row);

But have not got a result yet.

Please, help me to understand.


Try this:

$("#mainTable tr:first").after(row);

http://api.jquery.com/after/


InsertAfter是您正在寻找的内容:

var row='<tr><td>val</td><td>val2</td></tr>';
$(row).insertAfter("#mainTable tr:first");

You can use prepend JQuery method that inserts as the first child:

$("#mainTable tbody").prepend(row);

Prepend documentation

链接地址: http://www.djcxy.com/p/22980.html

上一篇: jQuery在第四行到最后一行之前添加行

下一篇: 在html表格的第一行之后追加行