jQuery 选择器 $('table td:eq(2) a') 仅从第一行获取单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2196697/
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
jQuery selector $('table td:eq(2) a') get the cell only from the first row
提问by Fitzchak Yitzchaki
$('table td:eq(2) a')
return the a
tag of the third column but only from the first row.
$('table td:eq(2) a')
返回a
第三列的标签,但只从第一行。
Why?
为什么?
回答by Matthew Manela
It is not a bug but it is definitely confusing. What will give you the result you expect is:
这不是错误,但绝对令人困惑。什么会给你你期望的结果是:
$('table td:nth-child(3) a')
While :nth-child and :eq seem very similar the behavior can be quite different as can be seen from the result you expected.
虽然 :nth-child 和 :eq 看起来非常相似,但从您预期的结果中可以看出,行为可能完全不同。
The jQuery documentation on this can be found here. It states:
可以在此处找到关于此的 jQuery 文档。它指出:
The :nth-child(n) pseudo-class is easily confused with :eq(n), even though the two can result in dramatically different matched elements. With :nth-child(n), all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class. With :eq(n) only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the nth one is selected.
:nth-child(n) 伪类很容易与 :eq(n) 混淆,尽管两者可能导致匹配元素截然不同。使用:nth-child(n),所有子元素都被计算在内,无论它们是什么,并且指定的元素仅在与附加到伪类的选择器匹配时才被选中。使用 :eq(n) 只计算附加到伪类的选择器,不限于任何其他元素的子元素,并且选择第 n 个。
In simpler words, eq(2) will select the third element in the while result set while :nth-child(3) will select the 3 child of its parent. And in this case the parent will be its tr.
简单来说, eq(2) 将选择 while 结果集中的第三个元素,而 :nth-child(3) 将选择其父级的第 3 个子元素。在这种情况下,父项将是它的 tr。
回答by Guffa
No, it's not a bug. It matches the anchor tag in third element in the set matched by table td
, so it's in the third cell in the table.
不,这不是错误。它与由 匹配的集合中第三个元素中的锚标记匹配table td
,因此它位于表中的第三个单元格中。
(If the table was only two cells wide, you would get the first cell in the second row.)
(如果表格只有两个单元格宽,您将获得第二行中的第一个单元格。)
回答by BrainCoder
In simple word,
简单来说,
According to your code $('table td:eq(2)')
returns third td
in table
as strting from index=0 it will pick third td
see below
根据你的代码 $('table td:eq(2)')
返回第三td
在table
从指数= 0 strting它将搭载第三td
,请参考下文
for $('table td:eq(4)')
result would be fifth td
of the table see below
对于$('table td:eq(4)')
结果将是第五个td
表见下文
to select whole second column use :nth-child() index starts from 1
选择整个第二列使用 :nth-child() 索引从 1 开始
ex: $('table td:nth-child(2)')
前任: $('table td:nth-child(2)')
I hope you get your answer.
我希望你能得到你的答案。
回答by Mohammad Ali Akbari
$('table td:eq(2)')
will select all 'table td'
, and the index eq(2)
will select the third td
from this collection. so there is just one a
under third column.
$('table td:eq(2)')
将选择 all 'table td'
,索引将从该集合中eq(2)
选择第三个td
。所以只有一个a
低于第三列。