简体   繁体   English

如何通过在html中使用id来获取表格行?

[英]How to get table row by using id in html?

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。任何人都可以帮助我

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.按照下面的评论中的要求添加了删除代码。

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>

如果要删除 id 为 b 的行

$("#b").remove()

If you have the Id within any cell, then the below should work如果您在任何单元格中都有 Id,则以下内容应该有效

document.getElementById("myID "+tempIdx).parentNode.parentNode.rowIndex;

provided table is in the below format提供的表格采用以下格式

<table>
<tr>
    <td>
        <p id="myID 1" />
    </td>
    <td>xxx</td>
</tr>
<td>yyy</td>
<td>
    <p id="myID 2" />
</td>
</tr>
<td>
    <p id="myID 3" />
</td>
</tr>
</table>

The first .parentNode will give the CELL the second will give the ROW , then get the INDEX from the ROW第一个.parentNode将给CELL第二个给ROW ,然后从ROW 中获取INDEX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM