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选择器选择第一个表的第一行或任何特定的表?
6 个解决方案
#1
33
$("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有第3个表,则获取第2个表中的任何tr
or
要么
$("table").eq(1).children("tr:has(table:eq(2))")
#2
5
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行。
#3
2
@svinto's answer is definitely the shortest, quickest and easiest way to accomplish this. If you're really concerned with performance (e.g. selecting within an arbitrary complex for loop), this might most likely prove to be a tad faster:
@ svinto的答案肯定是实现这一目标的最短,最快捷,最简单的方法。如果你真的关心性能(例如在一个任意复杂的循环中选择),这很可能会被证明有点快:
$('tr').eq(0)
If you need not use only jQuery selectors and actually require the DOM <TR>
element, you can use:
如果您不需要仅使用jQuery选择器并且实际需要DOM 元素,则可以使用:
$('table')[0].rows[0]
Alternatively:
或者:
$('tr')[0]
#4
1
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')
#5
0
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选择器。它非常详细但充满复杂的例子。
#6
0
$("tr:first");
#1
33
$("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有第3个表,则获取第2个表中的任何tr
or
要么
$("table").eq(1).children("tr:has(table:eq(2))")
#2
5
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行。
#3
2
@svinto's answer is definitely the shortest, quickest and easiest way to accomplish this. If you're really concerned with performance (e.g. selecting within an arbitrary complex for loop), this might most likely prove to be a tad faster:
@ svinto的答案肯定是实现这一目标的最短,最快捷,最简单的方法。如果你真的关心性能(例如在一个任意复杂的循环中选择),这很可能会被证明有点快:
$('tr').eq(0)
If you need not use only jQuery selectors and actually require the DOM <TR>
element, you can use:
如果您不需要仅使用jQuery选择器并且实际需要DOM 元素,则可以使用:
$('table')[0].rows[0]
Alternatively:
或者:
$('tr')[0]
#4
1
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')
#5
0
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选择器。它非常详细但充满复杂的例子。
#6
0
$("tr:first");