jQuery 获取“当前”表中的所有行,而不是从子表中获取

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

Get all rows in the "current" table, and not from child tables

jquery

提问by clarkk

How can you get all rows in a table without getting the rows in child tables?

如何在不获取子表中的行的情况下获取表中的所有行?

var rows = $('tr', tbl);

This will return ALL <tr>tags, including all rows in child tables.

这将返回所有<tr>标签,包括子表中的所有行。

回答by FishBasketGordo

var rows = $('#tblID > tbody > tr')

The child selectorwill get the table's <tbody>element and consequently get the <tr>elements that are direct children of the table's tbody.

子选择将获得该表的<tbody>元素,因此得到的<tr>是表的TBODY的直接子元素。

If you already have a table object:

如果您已经有一个表对象:

var rows = $(tbl).find('> tbody > tr');

Or:

或者:

var rows = $(tbl).children('tbody').children('tr');

Here is a working example.

这是一个工作示例

回答by Luty

var count = $('#tableID').rows;

It works, because the selector will return a HTMLTableElement object.

它有效,因为选择器将返回一个 HTMLTableElement 对象。

回答by Jorge Guberte

Probably:

大概:

var rows = $("#tableid>tr");

回答by Foram Shah

If you just want the count of rows, you can simply use:

如果你只想要行数,你可以简单地使用:

var table = document.getElementById('tableID');  
table.rows.length;  

Or you can use direct children selector:

或者您可以使用直接子选择器:

$('table > tbody > tr').each(function(index, tr) {} );