使用 jQuery 识别表 <tbody> 元素中的所有行

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

Identifying all rows within a table <tbody> element using jQuery

jqueryhtml-table

提问by cw84

I am trying to retrieve all rows from within a <tbody>section of a table, but am unsure of the syntax for doing so. I've included a dummy table extract below and my latest attempt for achieving the task with jQuery!

我试图从<tbody>表的一部分中检索所有行,但不确定这样做的语法。我在下面包含了一个虚拟表格摘录以及我使用 jQuery 完成任务的最新尝试!

Table extract:

表摘录:

<tbody>
 <tr>
  <th id="emergency" colspan="2">Emergency</th>
 </tr>
    <tr>
      <td>Emergency data</td>
      <td>Emergency data</td>
    </tr>
    <tr>
      <td>Emergency data</td>
      <td>Emergency data</td>
    </tr>
</tbody>
<tbody> 
 <tr>
  <th id="urgent" colspan="2">Urgent</th>
 </tr>
    <tr>
      <td>Urgent Data</td>
      <td>Urgent Data</td>
    </tr>
    <tr>
      <td>Urgent Data</td>
      <td>Urgent Data</td>
    </tr>
 </tbody>

jQuery code:

jQuery代码:

var emergencyRows = $table.find('#emergency').children().get();

回答by Ally

You can use the below if you know the table id.

如果您知道表 ID,则可以使用以下内容。

var trs = $("#tableid").find("tbody>tr");

回答by Omar

My suggestions is to place the ID attributes on the tbody rather than the first row of each one.

我的建议是将 ID 属性放在 tbody 上而不是每个的第一行。

HTML

HTML

<table>
    <tbody id="emergency">
        <tr>
            <th colspan="2">Emergency</th>
        </tr>
        <tr>
            <td>Emergency data</td>
            <td>Emergency data</td>
        </tr>
        <tr>
            <td>Emergency data</td>
            <td>Emergency data</td>
        </tr>
    </tbody>
    <tbody id="urgent">
        <tr>
            <th colspan="2">Urgent</th>
        </tr>
        <tr>
            <td>Urgent Data</td>
            <td>Urgent Data</td>
        </tr>
        <tr>
            <td>Urgent Data</td>
            <td>Urgent Data</td>
        </tr>
    </tbody>
</table>

jQuery

jQuery

var emergencyRows = $("tbody#emergency").find("tr:gt(0)");
var urgentRows = $("tbody#urgent").find("tr:gt(0)"); 

The jQuery snippet will get all the respective rows with the exception of the first rows.

jQuery 代码段将获取除第一行之外的所有相应行。

回答by Agent_9191

This is probably the cleanest

这可能是最干净的

$('#emergency').closest('tbody').children(); //only returns first child element, so the <tr>'s

回答by Annabelle

From your example, it seems like you might want "all rows except for the one containing #emergency". If that's the case, you can use the following:

从您的示例来看,您似乎想要“除包含 #emergency 的行之外的所有行”。如果是这种情况,您可以使用以下方法:

$('#emergency').closest('tr').siblings();

$('#emergency').closest('tr').siblings();

Note that #emergencyneed not be a <tr />or <th />or anything in particular. It could be any element within a table cell.

请注意,#emergency不必是一个<tr /><th />或任何特别的东西。它可以是表格单元格中的任何元素。

回答by richsage

Try:

尝试:

var emergencyRows = $("th#emergency").parent().parent().find("tr:gt(0)");

which should get you all rows that aren't the header row.

这应该为您提供所有不是标题行的行。

回答by Hasi

I was able to find a solution to get all records in paginated pages.You also can try out this.

我找到了一个解决方案来获取分页页面中的所有记录。你也可以试试这个。

var userList = $("#user-grid").dataTable().fnGetNodes();

var userList = $("#user-grid").dataTable().fnGetNodes();