无法在此33个元素的节点列表中选择第一个孩子

问题描述 投票:1回答:1

我将以图片形式说明这一点:

enter image description here

您可以看到这里有23个匹配的节点。

如何获得第一个?您会认为:

document.querySelectorAll('.vdatetimeCal > div > .cell.day:not(.day-header):nth-of-type(1)')

但是不...

返回空节点列表!

快速浏览HTML提供了一些线索:

enter image description here

啊哈-所以...你猜怎么着:

document.querySelectorAll('.vdatetimeCal > div > .cell.day:not(.day-header):nth-of-type(8)')

这可行!

谁能解释这是怎么回事?!

javascript css-selectors
1个回答
2
投票
document.querySelectorAll('.vdatetimeCal > div > .cell.day:not(.day-header)

返回不具有cellday day-header。但是,nth-of-type的伪类在其父级中指定标签类型的索引。您的第一个选择器具有两个互斥的伪类,因为nth-of-type(1)会产生一个div,其中classday-header:not排除。 nth-of-type(8)有效,因为您使用的伪类不会过滤掉span

© www.soinside.com 2019 - 2024. All rights reserved.