首页 > 解决方案 > 如何选择父级具有特定类的所有子级旁边的所有元素?

问题描述

我正在尝试选择不在 print 类中的所有元素。我想要一个真正选择每个元素的选择。(没有继承)。我怎样才能做到这一点?Javascript也很好。

所以3, 4, 5不应该是红色的。

body *:not(.print) {
  color: red;
}
<div>
  <!--this one-->
  1
  <!--this one-->
  <p>2</p>
  <!--this one-->
</div>
<div>
  <div class="print">
    3
    <p>4</p>
    <div>
      <p>5</p>
    </div>
  </div>
</div>
<div>
  <!--this one-->
  6
  <p>7</p>
  <!--this one-->
</div>

标签: javascripthtmlcss

解决方案


更新:

尝试这个:

html {
   color: red;
}

.print, .print * {
  color: black;
}

推荐阅读