[英]Why is my nth-child selector selecting the wrong element?
I have this element tree (stolen from Stack Overflow's 404 page): 我有这个元素树(从Stack Overflow的404页中窃取):
When we look at it, the highlighted <div>
is supposed to be the fourth element. 当我们查看它时,突出显示的<div>
应该是第四个元素。 However, doing $('body > div:nth-child(4)')
returns the div before. 但是,执行$('body > div:nth-child(4)')
返回之前的div。 Why is this so? 为什么会这样呢? Is is somehow selecting the div in the <noscript>
tag? 是否以某种方式在<noscript>
标记中选择div? When I remove the <noscript>
, it selects properly. 当我删除<noscript>
,它会正确选择。
Why is it behaving like that? 为什么会这样呢?
div:nth-child(4)
does not give you the 4th div
, it gives you the element that is both a div
and a 4th child (and gives you nothing if the 4th child is something that is not a div; that is why you are not getting anything for div:nth-child(1)
). div:nth-child(4)
不给您第4个div
,它给您既是div
又是第4个孩子的元素(如果第4个孩子不是div的东西,则什么也不会给您;这就是为什么您没有得到div:nth-child(1)
任何东西。 The 4th div that you want is the 5th child with <noscript>
present, and 4th child without it. 您想要的第4个div是存在<noscript>
的第5个孩子,而没有它的第4个孩子。
From jQuery docs : 从jQuery 文档 :
The
:nth-child(n)
pseudo-class is easily confused with:eq(n)
, even though the two can result in dramatically different matched elements.:nth-child(n)
伪类很容易与:eq(n)
混淆,即使两者可能导致匹配元素有很大不同。 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. 使用:nth-child(n)
,将计算所有子级,无论它们是什么,并且仅当指定元素与附加到伪类的选择器匹配时才选择指定元素。 With:eq(n)
only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the (n+1)th one (n is 0-based) is selected. 使用:eq(n)
仅对连接到伪类的选择器进行计数,而不仅限于其他元素的子级,并且选择第(n + 1)个选择器(n从0开始)。
So, if you want the 4th div
, you will do body > div:eq(3)
. 因此,如果需要第4个div
,则将执行body > div:eq(3)
。
:nth-child() :nth-child()
Selects all elements that are the nth-child of their parent. 选择作为其父项的第n个子项的所有元素。
jQuery's implementation of :nth-child(n) is strictly derived from the CSS specification jQuery的:nth-child(n)的实现严格源自CSS规范
W3C specification here W3C规范 在这里
The :nth-child( a n+ b ) pseudo-class notation represents an element that has a n+ b -1 siblings before it in the document tree 的:第n个孩子( 正 + b)的伪类表示法表示具有的n + B中的元素-1之前它的兄弟姐妹文档树
So here :nth-child(4)
present the full form :nth-child(0n+4)
, and denote an element has exactly 3 siblings before it in the document tree 因此,这里:nth-child(4)
呈现完整形式:nth-child(0n+4)
,并表示元素在文档树中正好具有3个同级元素
noscript tag Noscript标签
If enabled javascript, The <noscript>
tag may not be rendered but can still accessed using nextSibling
or prevSibling
, so it's counting with nth-child
如果启用了javascript,则<noscript>
标记可能无法呈现,但仍可以使用nextSibling
或prevSibling
访问,因此它以nth-child
计数
body > div
is combined with :nth-child
just like other pseudo-classes , eg a:hover
and tr:hover
, the :hover
is not concern whether it's a a
or tr
tag, just a special selector body > div
与:nth-child
组合在一起,就像其他伪类一样 ,例如a:hover
和tr:hover
, :hover
并不关心它是a
还是tr
标记,只是一个特殊的选择器
body>div:nth-child(4)
first select body>div
, and then select the div which has exactly 3 siblings before it, so the <noscript>
tag counting. body>div:nth-child(4)
首先选择body>div
,然后选择前有3个兄弟姐妹的div ,因此<noscript>
标记计数。
Try Changing div:nth-child(4)
to div:nth-child(5)
I've tested It and It works with 5th child not 4th child. 尝试将div:nth-child(4)
更改为div:nth-child(5)
我已经对其进行了测试,它适用于第5个孩子而不是第4个孩子。 With the 4th child It selects the <div>
before the one you want. 与第四个孩子一起选择它之前的<div>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.