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

2023-11-29前端开发问题
2

本文介绍了是否可以根据类名在没有 Javascript 的情况下更改悬停时的多个元素外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 divs 中有一个 divs 结构,类似于:

<div class="a">你好</div><div class="a">堆栈</div><div>溢出</div></div>

<div>你</div><div class="b">是</div><div class="b">最好的</div></div>

<div>有</div><div class="b">一个不错的</div><div>日!!</div></div></div>

我希望所有具有类 adiv 在其中一个被鼠标悬停时更改背景颜色.对于所有具有类 bdiv 都相同:当悬停具有类 bdiv 之一时,所有divs with class b 应该改变背景颜色.

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

如果答案是否定的:
如果知道所有具有类 adiv 都是连续的 div 是否可能在同一级别(即兄弟姐妹)?

如果需要,我还可以将其他类添加到 divs.

解决方案

在没有容器 <div>s 的更简单的情况下,你可以让它工作一半":

<div class="a">你好</div><div class="a">堆栈</div><div>溢出</div><div class="b">是</div><div class="b">最好的</div><div>有</div><div class="b">一个不错的</div><div>日!!</div></div>

然后你可以使用 通用兄弟组合器, 不幸的是,它仅适用于左侧描述的元素之后的元素.因此,例如,如果您将鼠标悬停在包含The Best"的 <div> 上,那么只有那个和a nice"的 <div> 会发生变化背景:

div.b:hover, div.b:hover ~ div.b {背景颜色:#CCCCCC;}

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

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.

解决方案

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.

这篇关于是否可以根据类名在没有 Javascript 的情况下更改悬停时的多个元素外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13