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 演示
这篇关于将样式应用于相似元素行中的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将样式应用于相似元素行中的第一个元素


基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01