考虑以下 HTML:
<div class="status-date">
<strong>Date Available:</strong>
10/05/2016
</div>
我希望 :not()
选择器能够定位日期字符串“10/05/2016”,如下所示:
.status-date *:not(strong) {
text-decoration: underline;
}
两个问题:
1.是:not()
选择器能够做到这一点吗?
2. 如果没有,任何 CSS 选择器都可以做到这一点吗?
上下文:这实际上与文本节点的样式无关。我正在做一些网络抓取,我想忽略 <strong>
在这种情况下标记。如果是关于样式,我可以定位 div
直接覆盖 <strong>
上的样式“取消它”。
更多背景:我可以看到我的天真的尝试没有按预期工作。例如,如以下代码笔所示:http://codepen.io/anon/pen/rWezQK 但我可能误解了有关选择器或我所描述的 DOM 结构的深层内容。
最佳答案
简单的选择器代表元素。对于所有简单选择器都是如此,包括 *
和 :not()
。文本包含在元素中,但其本身并不是元素。您将无法仅将文本与任何 CSS 选择器“匹配”,因为就选择器而言,DOM 所谓的文本节点甚至不存在于文档树中。
The specification itself offers only three lines on the
:not()
selector.
规范中的第一行支持这一点:
The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument.
请注意,它说“它代表一个元素”。
如果您正在进行网页抓取,请考虑 XPath:
//div[contains(concat(' ', @class, ' '), ' status-date ')]/strong/following-sibling::text()
关于html - 是否可以使用:not() selector to target specific text nodes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569934/