是否可以根据类名在没有 JavaScript 的情况下更改悬停时的多个元素外观?

发布于 2024-09-18 20:12:08 字数 1009 浏览 3 评论 0 原文

我在 div 内有一个 div 的结构,类似于:

<div>
    <div>
        <div class='a'>Hello</div>
        <div class='a'>Stack</div>
        <div>Overflow</div>
    </div>
    <div>
        <div>You</div>
        <div class='b'>Are</div>
        <div class='b'>The Best</div>
    </div>
    <div>
        <div>Have</div>
        <div class='b'>a nice</div>
        <div>Day !!</div>
    </div>
</div>

我想要所有带有 a 类的 div > 当鼠标悬停其中之一时更改背景颜色。对于所有具有 b 类的 div 也是如此:当悬停具有 b 类的 div 之一时,所有具有 b 类的 div 应该更改背景颜色。

是否可以无需 Javascript 实现此行为?

如果答案是否定的:
如果已知所有具有 a 类的 div 都是同一级别中的连续 div,这可能吗(即兄弟姐妹)?

如果需要的话,我还可以向 div 添加其他类。

I have a structure of divs inside divs, something like:

<div>
    <div>
        <div class='a'>Hello</div>
        <div class='a'>Stack</div>
        <div>Overflow</div>
    </div>
    <div>
        <div>You</div>
        <div class='b'>Are</div>
        <div class='b'>The Best</div>
    </div>
    <div>
        <div>Have</div>
        <div class='b'>a nice</div>
        <div>Day !!</div>
    </div>
</div>

I would like all divs with class a to change the background color when one of them is hovered by mouse. The same for all divs with class b: when one of divs with class b is hovered, all divs with class b should change the background color.

Is that possible to implement this behavior without Javascript ?

If the answer is no:
Is that possible if known that all divs with class a are consecutive divs in the same level (i.e. siblings) ?

I can add also other classes to divs, if needed.

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

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

发布评论

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

评论(3

仄言 2024-09-25 20:12:08

在没有容器

的更简单情况下,您可以让它“半工作”:

<div>
  <div class='a'>Hello</div>
  <div class='a'>Stack</div>
  <div>Overflow</div>
  <div class='b'>Are</div>
  <div class='b'>The Best</div>
  <div>Have</div>
  <div class='b'>a nice</div>
  <div>Day !!</div>
</div>

然后您可以使用 通用兄弟组合器,不幸的是它只适用于元素之后的元素描述在左侧。因此,例如,如果您将鼠标悬停在包含“The Best”的

上,则只有该内容和“a Nice”

才会发生变化背景:
div.b:hover, div.b:hover ~ div.b {
    background-color:#CCCCCC;
}

不过,我无法想出一种仅通过 CSS 就能完全处理您的场景的方法。我倾向于其他人所说的关于现在不可能(即使在简化的情况下)的说法。

You can get it "half working" in the simpler case where there are no container <div>s:

<div>
  <div class='a'>Hello</div>
  <div class='a'>Stack</div>
  <div>Overflow</div>
  <div class='b'>Are</div>
  <div class='b'>The Best</div>
  <div>Have</div>
  <div class='b'>a nice</div>
  <div>Day !!</div>
</div>

Then you could use the general sibling combinator, with the unfortunate caveat that it only works for elements that come after the element described on the left-hand side. So, for example, if you hovered over the <div> containing "The Best", only that and the "a nice" <div> would have a changed background:

div.b:hover, div.b:hover ~ div.b {
    background-color:#CCCCCC;
}

I wasn't able to come up with a way that would fully take care of your scenario through CSS alone, though. I'm leaning towards what the others have said about it not being possible (even in the simplified case) right now.

失去的东西太少 2024-09-25 20:12:08

我想不出任何解决方案,除了有 css-parent-selectors (而且,据我自己和谷歌所知,那些不存在)。如果存在类似的情况,您可以执行诸如选择悬停元素的顶部父级之类的操作,然后选择该顶部元素中类的所有元素(看起来像 .a ) - 但是,如上所述,这个选择器不存在,所以你的问题的答案是:

i can't think of any solution, except there are css-parent-selectors (and, as far as myself and google know, those don't exist). if there would be things like that, you could do something like selecting the top parent af the hovered element and then select all elements of your class within that top-element (would look like .a < parent < parent < parent .a{ /*styles*/ }) - but, as said, this selectors don't exists, so the answer to you question is: no

╰ゝ天使的微笑 2024-09-25 20:12:08

不,没有 Javascript 就不行。 CSS 选择器旨在将样式应用于单独匹配选择器的每个元素,因此根据设计,这种情况不会发生。

来源

http://www.w3.org/TR/CSS2/selector .html#选择器语法

No. Not without Javascript. CSS selectors are meant to apply styles to each element that matches the selector individually, so by design this won't happen.

Source

http://www.w3.org/TR/CSS2/selector.html#selector-syntax

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