html - 选择每个
不是其父元素的第一个子元素的元素
问题描述
如果我想选择<p>
不是其父元素的第一个元素的每个元素,那么正确的 CSS 选择器是什么?
就像下面的这个片段,只是反转!(所以“第二”、“第三”、“第四”是红色的,“第一”是正常的)
p:first-of-type {
background: red;
}
<body>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</body>
解决方案
您可以使用:not
否定伪类。请注意,在组合伪类时,您必须将第二个伪类放在括号内:not(:first-of-type)
:
p:not(:first-of-type) {
background: red;
}
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
请注意,如果您特别希望选择元素的第一个子元素以外的所有元素,则可以使用:not(:first-child)
. 请注意,在这种情况下,选择器位于子元素上,而不是父元素:
.parent p:not(:first-child) {
background: red;
}
<div class="parent">
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</div>
推荐阅读
- python - 如何将以下 JSON 数据加载到 Pyspark 中,并为所选属性的每个值分别设置行
- postgresql - 在 PostgreSQL 中使用全文搜索,我怎样才能使某些词不值得匹配?
- python - 使用关键字提取在 pandas 中动态创建列
- html - 在 VBScript 中使用 MSHTML 解析带有 Unicode 字符的 HML 文件
- sql - SQL JOIN to find mismatches
- javascript - 如何从 Yahoo API JSON 获取天气图标?
- python - Unable to paginate with Selenium and Scrapy
- excel - vba中的Vlookup查找两个工作表之间的值
- java - How to print int new line in java?
- android - Request Push Notification Permissions for Cordova iOS and Android?