如何使用 jquery 或 javascript 删除索引处的一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5821719/
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
How to use jquery or javascript to remove a row at index?
提问by Varia
Possible Duplicate:
What is the best way to remove a table row with jQuery?
可能的重复:
使用 jQuery 删除表格行的最佳方法是什么?
If I have table with 3 rows using tr
tag, how do I remove the first row (index 0) with either jquery or some other javascript?
如果我有 3 行使用tr
标记的表,如何使用 jquery 或其他一些 javascript 删除第一行(索引 0)?
回答by Cristian
try
尝试
$("tr").eq(0).remove();
However, like @Gregg said, take a look at official docs
但是,就像@Gregg 所说的,看看官方文档
回答by pixelbobby
This would remove the 3rd row in a table.
这将删除表中的第三行。
$("table tr:eq(2)").remove();
Also, don't forget to use <thead>
and <tbody>
in your <table>
. It makes things more accessible and helps if you want to use a plug-in for sorting later down the road.
另外,不要忘了使用<thead>
,并<tbody>
在你的<table>
。如果您想在以后使用插件进行分类,它会使事情变得更容易访问并提供帮助。
回答by gilly3
Here's the "some other javascript" approach, which is itself very simple:
这是“其他一些 javascript”方法,它本身非常简单:
document.getElementById("myTable").deleteRow(0);
回答by Gregg
The jQuery docsare a great resource for this kind of thing...
在jQuery的文档是这种事情一个很好的资源...
$("tr:first").remove()