Html 如何通过在html中使用id来获取表格行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19224866/
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 get table row by using id in html?
提问by user2767541
How to get tr count from html table by using Id.Please check my code below
如何使用 Id 从 html 表中获取 tr 计数。请检查下面的代码
<table id="tableId">
<tr id="a">
<td>a</td>
</tr>
<tr id="b">
<td>b</td>
</tr>
Now I want to find rowNumber by using row Id.for example my rowId is 'b' I want to find rowNumber for this Id.Any one help me
现在我想通过使用行 ID 来查找 rowNumber。例如我的 rowId 是 'b' 我想找到这个 Id 的 rowNumber。任何人都可以帮助我
回答by om_deshpande
Here you go
干得好
var table = document.getElementById("tableId");
var rowIndex = document.getElementById("b").rowIndex;
table.deleteRow(rowIndex);
Added the code for deletion, as requested in the comments below.
按照下面的评论中的要求添加了删除代码。
回答by Mandip Darji
It's very using with jquery
它非常适合与 jquery 一起使用
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var rowCount = $('table#tableId tr:#b').index() + 1;
alert(rowCount);
$("#b").remove() // For Remove b Row (tr)
});
</script>
Your HTML
你的 HTML
<table id="tableId">
<tr id="a">
<td>a</td>
</tr>
<tr id="b">
<td>b</td>
</tr>
</table>
回答by Madhur Ahuja
If you want to remove the row with id b
如果要删除 id 为 b 的行
$("#b").remove()