如何使用JavaScript从表中的行Index获取行id [英] How to get row id from row Index in table using JavaScript

查看:78
本文介绍了如何使用JavaScript从表中的行Index获取行id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这是我的桌子:

<table>
    <tr id="a">
       <TD>a</TD>
    </tr>
    <tr id="b">
       <TD>b</TD>
    </tr>
</table>

如何使用表格中的行索引获取行ID?

How can I get row id using the row index from a table?

上面只是一个例子,其中id是静态的但在我的情况下我的id是动态的,所以我不能使用
document.getElementById()

Above is just an example where id is static but in my case my id is dynamic, so I can't use document.getElementById().

推荐答案

假设您的页面上只有一个表格:

Assuming you have only one table on your page:

document.getElementsByTagName("tr")[index].id;

优选但是,你会给你的一个 id ,然后得到你的行:

Preferably though, you'd give your table a id, though, and get your row like this:

<table id="tableId">
    <tr id="a">
        <td>a</td>
    </tr>
    <tr id="b">
        <td>b</td>
    </tr>
</table>





var table = document.getElementById("tableId");
var row = table.rows[index];
console.log(row.id);

这样,如果您有多个表,您可以确定不会受到任何干扰你的页面。

This way, you can be certain you don't get any interference, if you have multiple tables in your page.

这篇关于如何使用JavaScript从表中的行Index获取行id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆