Apply style to first element in a row of similar elements(将样式应用于相似元素行中的第一个元素)
问题描述
我有以下列表(数字仅供参考)
I have the following list (the numbers are just for reference)
<div class="A">alpha1</div>
<div class="B">alpha2</div>
<div class="A">alpha3</div>
<div class="A">alpha4</div>
<div class="A">alpha5</div>
<div class="B">alpha6</div>
<div class="A">alpha7</div>
我想将一种样式应用于 DIVS 1、3 和 7,因为它们是同一类的一行元素中它们的类 (A) 中的第一个.有没有我可以使用的伪元素/魔法?类似(发明)
I want to apply one style to DIVS 1, 3 and 7, because they are the first of their class (A) in a row of elements of the same class. Is there a pseudo element / magic I can use for that? Something like (inventing)
not(.A) & .A {color:red} -> if class is A and it is not preceded by an A
谢谢!
推荐答案
你使用 :not()
伪类和相邻的兄弟组合 +
来匹配一个.A
前面没有紧跟 .A
:
You use the :not()
pseudo-class with an adjacent sibling combinator +
to match an .A
that is not immediately preceded by an .A
:
:not(.A) + .A
您还需要使用 :first-child
来选择第一个 .A
元素,因为它前面没有任何内容:
You'll also need to use :first-child
to select the very first .A
element since it's not preceded by anything:
.A:first-child
把它们结合起来,你就有了:
Combine them, and you have:
:not(.A) + .A, .A:first-child { color: red; }
jsFiddle 演示
这篇关于将样式应用于相似元素行中的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将样式应用于相似元素行中的第一个元素
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01