jQuery:通过tr'id'获取行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13641553/
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 12:49:43  来源:igfitidea点击:

jQuery : Get row by tr 'id'

jquery

提问by Mikkel Nielsen

Hey im trying to access a specific row. I have the id attr of the tr I wan't to get.

嘿,我正在尝试访问特定行。我有我不想得到的 tr 的 id attr。

My table looks like this:

我的桌子看起来像这样:

<tbody role="alert" aria-live="polite" aria-relevant="all">
    <tr id="1" class="odd">
        <td class=" sorting_1">bla</td>
    </tr>
    <tr id="3" class="even">
        <td class=" sorting_1">hej</td>
    </tr>
    <tr id="4" class="odd row_selected">
        <td class=" sorting_1">sdf</td>
    </tr>
    <tr id="8" class="even">
        <td class=" sorting_1">testgfdgvcxbvcbcv</td>
    </tr>
    <tr id="9" class="odd">
        <td class=" sorting_1">testgfdgvcxbvcbcvfdgsgfdgdfs</td>
    </tr>
</tbody>

Here i need to access But I can't figure out how to access this.

这里我需要访问但我不知道如何访问它。

回答by Jamiec

Well, you should usually avoid having numeric-only id's (unless you are using 100% HTML5!). And you should definatelymake sure id's are unique on your page.

好吧,您通常应该避免使用纯数字 ID(除非您使用的是 100% HTML5!)。并且您一定要确保 id 在您的页面上是唯一的。

But you should be able to use jquery selector

但是你应该能够使用 jquery 选择器

$('#1')

Assuming you're not using HTML5, then you should change your markup to use a more standard ID:

假设您没有使用 HTML5,那么您应该更改标记以使用更标准的 ID:

<tbody role="alert" aria-live="polite" aria-relevant="all">
    <tr id="row1" class="odd">
        <td class=" sorting_1">bla</td>
    </tr>
    <tr id="row3" class="even">
        <td class=" sorting_1">hej</td>
    </tr>
    ...

Then you would access a particular row using

然后您将使用访问特定行

$('#row1')

回答by akohout

You can retrieve any element, where you have the ID, using

您可以使用具有 ID 的任何元素检索

$("#ID")

So in your case it's

所以在你的情况下

$("#1")

This will return you the jQuery Object of the underlying HTML element, in your case the tr element.

这将返回底层 HTML 元素的 jQuery 对象,在您的情况下是 tr 元素。