【问题标题】:css3 selectors and pseudo-classes [duplicate]css3选择器和伪类[重复]
【发布时间】:2016-11-30 08:01:15
【问题描述】:

请解释为什么在某些情况下伪类与 CSS 选择器一起工作而有时不工作。

守则 (https://jsfiddle.net/aspanoz/m1sg4496/):

 
div.wrapper {
  margin-bottom: 10px
}
div[class*="myclass"]:not(:first-of-type) {
  color: green
}
div[class*="myclass"]:first-of-type {
  color: red
}
<div class="wrapper">
  <div class="myclass">This text should appear red.</div>
  <div class="myclass">This text should appear green.</div>
  <div class="myclass">This text should appear green.</div>
  <div>this :first-of-type working as exepted</div>
</div>
<div class="wrapper">
  <div>but this :first-of-type fail</div>
  <div class="myclass">This text should appear red.</div>
  <div class="myclass">This text should appear green.</div>
  <div class="myclass">This text should appear green.</div>
</div>

【问题讨论】:

  • 您可能希望将其分解为易于管理的部分,而不是一次性转储一大堆示例,
  • Paulie_D,这看起来可以管理吗?
  • 附带说明:为什么使用属性选择器而不是类选择器(点)?我很确定以这种方式使用的属性选择器更昂贵。
  • 我从代码的一个工作示例中制作了这个,其中需要使用选择器。
  • 这个问题已经被问了大约一百次了。请更加努力地寻找答案。另请参阅我链接为重复的问题。

标签: css css-selectors pseudo-class


【解决方案1】:

:first-of-type 伪类名称中的单词“type”指的是标签类型(即标签name),而不是它前面的选择器。

官方定义(来自Selectors Level 3)是:

与:nth-of-type(1) 相同。 :first-of-type 伪类表示一个元素,它是其父元素的子元素列表中其类型的第一个兄弟元素。

这是您选择的选择器。

div[class*="myclass"]:first-of-type

此代码选择满足两个以下条件的元素。

  1. class 属性必须包含“myclass”,并且
  2. 它必须是其容器中的第一个 div 元素。

在您的第二个示例中,此选择器导致查找失败,因为没有符合这两个条件的元素。

存在一些 CSS hack,它只能选择与您的选择器匹配的第一个元素。我这里的代码基于this very thorough answer。这取决于general sibling selector 和CSS specificity 的性质。

div.wrapper {
  margin-bottom: 10px
}

/* first select all of your items */
div[class*="myclass"] {
  /* apply the "special" styling here */
  color: red;
}

/* use general sibling selector to grab all except the first */
div[class*="myclass"] ~ div[class*="myclass"] {
  /* "undo" your style on these */
  color: green;
}
<div class="wrapper">
  <div>This text is unaffected.</div>
  <div class="myclass">This text should appear red.</div>
  <div class="myclass">This text should appear green.</div>
  <div class="myclass">This text should appear green.</div>
</div>

<div class="wrapper">
  <div class="myclass">This text should appear red.</div>
  <div class="myclass">This text should appear green.</div>
  <div class="myclass">This text should appear green.</div>
  <div>This text is unaffected.</div>
</div>

<div class="wrapper">
  <div class="myclass">This text should appear red.</div>
  <div class="myclass">This text should appear green.</div>
  <div>This text is unaffected.</div>
  <div class="myclass">This text should appear green.</div>
</div>

【讨论】:

  • 谢谢。我认为它在 Linux shell 中用作管道
  • @Konstantin 我刚刚添加了一个示例,可以满足您的要求。
  • 我同意,它适用于我的示例。但是,如果课程不直接进行,它将无法正常工作
  • @Konstantin 它们需要处于相同的 DOM 级别,但它们不需要是直接相邻的节点。
  • 哇!你帮了我很多,你消除了我对伪类和选择器工作的误解)并展示了正确的方法。再次感谢你。祝你好运
【解决方案2】:

如下:

div[class*="myclass"]:first-of-type {
  color: red
}

<div class="wrapper">
  <div>but this :first-of-type fail</div>
  <div class="myclass1">This text should appear red.</div>
  <div class="myclass1">This text should appear green.</div>
  <div class="myclass1">This text should appear green.</div>
</div>

您是在告诉浏览器查找div,其中:

i) 是第一个div; 和

ii) 有一个以myclass 开头的类。

第一个 div 是没有 .myclass1 的那个 - 因此,它不是红色的。

.myclass1 的第一次出现在 第二个 div - 要正确选择该 div,您需要使用:

div[class*="myclass"]:nth-of-type(2)

为什么:first-of-type 不起作用?

type 仅指元素的类型。

您正在寻找的是:first-of-query。

到目前为止,:first-of-query 还不存在。

【讨论】:

  • 什么是first-of-query?有没有关于这种伪类的提议?
  • 我脑子里有个关于这样一个伪类的建议。
  • 嗯。我不知道为什么上面的答案得到了反对票。
  • 不是我,如果这就是你的意思。无论如何,您可能想知道有一个名为nth-of-match() 的官方提案。
  • 不,我没想到是你,不用担心。它只是激起了我的好奇心,我正在大声思考。感谢:nth-match() 引起我注意的提醒(!) - 不过,我从@BoltClock(此处:stackoverflow.com/questions/21167159/css-nth-match-doesnt-work)的回答中了解到:nth-match() 不再存在。就个人而言,我——我相信还有很多其他人——会喜欢看到:nth-of-query()伪选择器。
猜你喜欢
  • 1970-01-01
  • 2013-08-30
  • 2014-07-21
  • 2012-06-30
  • 1970-01-01
  • 2017-06-20
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多