如何选择除特定div内的元素以外的所有元素

时间:2019-10-11 08:38:12

标签: html css css-selectors

这是我的标记:

 <div class="car">
    <div class="make">NISSAN</div>
    <div class="model">MICRA</div>
</div>
<div class="discontinued">
    <div class="car">
        <div class="make">FORD</div>
        <div class="model">MONDEO</div>
    </div>
</div>
<div class="car">
    <div class="make">HONDA</div>
    <div class="model">INTEGRA</div>
</div>
</div>
<div class="car">
    <div class="make">PEUGEOT</div>
    <div class="model">206</div>
</div>
<div class="car">
    <div class="make">TOYOTA</div>
    <div class="model">COROLLA</div>
</div>

我想找到一种方法来选择除discontinued类的div外的所有汽车品牌。

我尝试了以下操作:

div:not(.discontinued) .make

...但是它仍然包括FORD

1 个答案:

答案 0 :(得分:4)

选择.car内的所有.discontinued(您可以使用:not pseudo-class selectordirect child selector进行选择),然后选择其中的所有.make

:not(.discontinued) > .car .make {
  color: red
}
<div class="car">
  <div class="make">NISSAN</div>
  <div class="model">MICRA</div>
</div>
<div class="discontinued">
  <div class="car">
    <div class="make">FORD</div>
    <div class="model">MONDEO</div>
  </div>
</div>
<div class="car">
  <div class="make">HONDA</div>
  <div class="model">INTEGRA</div>
</div>
<div class="car">
  <div class="make">PEUGEOT</div>
  <div class="model">206</div>
</div>
<div class="car">
  <div class="make">TOYOTA</div>
  <div class="model">COROLLA</div>
</div>