jQuery 如何使用jQuery选择html页面中第一个表格的第一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/838880/
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
How to select first row of the first table in an html page using jQuery?
提问by Chad Grant
Suppose if I have multiple tables in my HTML page (without their 'id' attribute), so how can I select first row of the first table or any specific table using jQuery selectors?
假设我的 HTML 页面中有多个表(没有它们的“id”属性),那么如何使用 jQuery 选择器选择第一个表的第一行或任何特定表?
回答by Chad Grant
$("table:first > tr:first")
or
或者
$("table:first").find("tr:first")
or
或者
$("table:first").children("tr:first")
or
或者
$("table").eq(0).children("tr").eq(0)
So if I understand the followup question...
所以如果我理解后续问题......
$("table:eq(1) tr:has(table:eq(2))")
translates to: get any tr's in the 2nd table's if the tr has a 3rd table
转换为:如果 tr 有第三个表,则获取第二个表中的任何 tr
or
或者
$("table").eq(1).children("tr:has(table:eq(2))")
回答by Thomas Stock
Something you can use to select the nth row in the nth table:
您可以用来选择第 n 个表中的第 n 行:
$("table:eq(n) tr:eq(n)")
with n being the zero based index of the table or tr.
n 是表或 tr 的从零开始的索引。
Example:
例子:
$("table:eq(2) tr:eq(4)")
gets the 5th row of the 3rd table.
获取第 3 个表的第 5 行。
回答by WynandB
@svinto's answer is definitely the shortest, quickest and easiest way to accomplish this. If you're reallyconcerned with performance (e.g. selecting within an arbitrary complex forloop), this might most likely prove to be a tad faster:
@svinto 的答案绝对是实现这一目标的最短、最快和最简单的方法。如果您真的关心性能(例如在任意复杂的for循环中进行选择),这很可能会被证明要快一点:
$('tr').eq(0)
If you need not use onlyjQuery selectors and actually require the DOM <TR>
element, you can use:
如果您不仅需要使用jQuery 选择器并且实际上需要 DOM<TR>
元素,您可以使用:
$('table')[0].rows[0]
Alternatively:
或者:
$('tr')[0]
回答by duckyflip
Using jQuery's eq()
method you can specify the index of the element you want to get.
This will select the first row of the second table found in the DOM
使用 jQuery 的eq()
方法,您可以指定要获取的元素的索引。
这将选择在 DOM 中找到的第二个表的第一行
$('table:eq(1) tr:first')
回答by svinto
$("tr:first");
回答by Josh
Although not jQuery specific, I was introduced to dom selectors at this w3c selectors page. It's very detailed but full of intricate examples.
虽然不是特定于 jQuery 的,但我在这个w3c 选择器页面上被介绍了 dom 选择器。它非常详细,但充满了复杂的例子。