问题描述
我在 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>我希望所有具有类 a 的 div 在其中一个被鼠标悬停时更改背景颜色.对于所有具有类 b 的 div 都相同:当悬停具有类 b 的 div 之一时,所有divs with class b 应该改变背景颜色.
是否有可能实现此行为无需 Javascript?
如果答案是否定的:
如果知道所有具有类 a 的 div 都是连续的 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
相关推荐
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //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
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09
前端开发问题
313
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
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
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 always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)