针对空类

时间:2018-01-19 10:43:01

标签: css css3

我想将green backgroundred一个应用于其他所有段落。我已经完成了一半,但仍在努力处理空 class=""

p {background:green}

#test p:not([class]) {
  background: red;
  color: #fff;
}
<div id="test">
  <p>This should be red</p>
  <p class="abc">This should be green</p>
  <p class="newname">This should be green</p>
  <p class="">This should be red</p>
  <p class="anothername">This should be green</p>
  <p>This should be red</p>
  <p class="">This should be red</p>
</div>

Fiddle

有人知道吗?

2 个答案:

答案 0 :(得分:6)

你可以使用这样的空类来定位p ......

p[class=""] {
    ...
}

body {
  font-weight: 700;
}

p {
  background: green
}

#test p:not([class]),
#test p[class=""] {
  background: red;
  color: #fff;
}
<div id="test">
  <p>This should be red</p>
  <p class="abc">This should be green</p>
  <p class="newname">This should be green</p>
  <p class="">This should be red</p>
  <p class="anothername">This should be green</p>
  <p>This should be red</p>
  <p class="">This should be red</p>
</div>

答案 1 :(得分:1)

我使用了#test p [class =“”]我希望这就是你想要的。

body{
  font-weight:700;
}
p{background:green}

#test p:not([class]) {
  background:red;
  color:#fff;
}

#test p[class=""]{
  background:red;
  color:#fff;
}