【发布时间】:2014-04-24 17:36:10
【问题描述】:
我有以下 html,我只想显示第一个“.xpto”元素。 我的问题是我正在动态删除类 .xpto,所以当我这样做时,第一个 .xpto 元素将是第二个内部 div。我怎样才能做到这一点?
<div>
<div class="xpto"></div>
<div class="xpto"></div>
<div class="xpto"></div>
<div class="xpto"></div>
</div>
我尝试了 :first-child、:first-of-type 和 :nth-child(1n) 但没有任何效果。 有任何想法吗? (仅限 CSS)
【问题讨论】:
-
难道没有
:nth-of-type()伪选择器吗? -
很抱歉,您能否再解释一下。你的例子让我更加困惑。
-
哦,没关系。
:nth-of-type()和:first-of-type()仅适用于元素标签,不适用于类或 ID。 -
.. 和 stackoverflow.com/questions/3615518/… .. 见 BoltClock 的答案。
标签: html css css-selectors