jQuery 在 html 表中的第一行之后追加行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2456737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:33:05  来源:igfitidea点击:

append row after first row in a html table

jqueryhtml-table

提问by loviji

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

我已经var row=<tr><td>val</td><td>val2</td></tr>尝试过这个:

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

but it appends to the end of the table.

但它附加到表的末尾。

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

我也试过 $("#mainTable tr:first").after().append(row);

But have not got a result yet.

但是还没有得到结果。

Please, help me to understand.

请帮我理解。

采纳答案by Thiago Belem

Try this:

尝试这个:

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

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

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

回答by Dexter

InsertAfteris what you are looking for:

InsertAfter是您要查找的内容:

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

回答by Diego Cotini

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

您可以使用作为第一个子项插入的 prepend JQuery 方法:

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

Prepend documentation

前置文件