2022年有可能生兄弟姐妹吗?

发布于 2025-01-15 09:04:51 字数 1934 浏览 3 评论 0原文

参考 2009 年的 Stackover 问题(是否有“前一个兄弟”选择器?),当时看来是不可能的。

问题

  1. 这里有两个小例子,说明了CSS 触及的两个元素都位于触发元素下方的
  2. 。在示例二中,一个元素位于触发元素上方,而另一个元素保持在触发元素下方。因此,同级选择器不会影响顶部的元素。

示例一

.toggle-switch {
  padding:50px;
}
#nocheck {
  margin-bottom: 2px;
} 

#chkTest:checked ~ #check { color: green; }
#chkTest:checked ~ #nocheck { color: black; }
#check { color: black; }
#nocheck { color: blue; }
  <div class="">
    <div class="toggle-switch">
      
      <input type="checkbox" id="chkTest" name="chkTest">
      <label for="chkTest">
        <span class="toggle-track"></span>
      </label>
      <div class="" id="nocheck">ENABLE</div>
      <div class="col-3 col-md-3" id="check">DISABLE</div>
      
    </div>
  </div>

示例2

.toggle-switch {
  padding:50px;
}
#nocheck {
  margin-bottom: 2px;
} 

#chkTest:checked ~ #check { color: green; }
#chkTest:checked ~ #nocheck { color: black; }
#check { color: black; }
#nocheck { color: blue; }
  <div class="">
    <div class="toggle-switch">
      <div class="" id="nocheck">ENABLE</div>
      <input type="checkbox" id="chkTest" name="chkTest">
      <label for="chkTest">
        <span class="toggle-track"></span>
      </label>
      
      <div class="col-3 col-md-3" id="check">DISABLE</div>
      
    </div>
  </div>

Referring to this Stackover question from 2009 (Is there a "previous sibling" selector?), it seems that it was not possible then.

Here are two small examples that illustrate the problem

  1. both elements touched by the CSS are under the triggering element.
  2. In example two one Element is above the triggering element and the other remains below it. As a result, the sibling selector does not affect the element on top.

Example one

.toggle-switch {
  padding:50px;
}
#nocheck {
  margin-bottom: 2px;
} 

#chkTest:checked ~ #check { color: green; }
#chkTest:checked ~ #nocheck { color: black; }
#check { color: black; }
#nocheck { color: blue; }
  <div class="">
    <div class="toggle-switch">
      
      <input type="checkbox" id="chkTest" name="chkTest">
      <label for="chkTest">
        <span class="toggle-track"></span>
      </label>
      <div class="" id="nocheck">ENABLE</div>
      <div class="col-3 col-md-3" id="check">DISABLE</div>
      
    </div>
  </div>

Example 2

.toggle-switch {
  padding:50px;
}
#nocheck {
  margin-bottom: 2px;
} 

#chkTest:checked ~ #check { color: green; }
#chkTest:checked ~ #nocheck { color: black; }
#check { color: black; }
#nocheck { color: blue; }
  <div class="">
    <div class="toggle-switch">
      <div class="" id="nocheck">ENABLE</div>
      <input type="checkbox" id="chkTest" name="chkTest">
      <label for="chkTest">
        <span class="toggle-track"></span>
      </label>
      
      <div class="col-3 col-md-3" id="check">DISABLE</div>
      
    </div>
  </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2025-01-22 09:04:51

这与父选择器有同样的问题,CSS 只能看到 DOWN DOM 而不能备份。因为它只能看到未来的兄弟姐妹、孩子、孩子的孩子等。

我认为 Safari 在其开发版本之一中实现了 :has() 伪类,但它们是一个小型浏览器,而 Chrome 尚未实现任何内容。

您可能会被这里的几个问题所欺骗,这可能是一个有用的问题:
有 CSS 父选择器吗?

Kevin Powell 有一个很棒的视频
https://www.youtube.com/watch?v=2-xdcDsqsAs

It's the same issue as with a parent selector, CSS can only see DOWN the DOM not back up. As in it can only see future siblings, children, children of children etc.

Safari I think has implemented the :has() pseudo class in one of their dev versions but they are a small browser and Chrome has yet to implement anything.

You might get duped as a few questions on here, this is probably a useful one:
Is there a CSS parent selector?

Kevin Powell has a great video
https://www.youtube.com/watch?v=2-xdcDsqsAs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文