使用 :hover 修改另一个类的css?

2023-11-01前端开发问题
1

本文介绍了使用 :hover 修改另一个类的css?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当只使用 css 将鼠标悬停在另一个类的元素上时,有没有办法修改一个类的 css ?

Is there a way to modify the css for one class when hovering on an element from another class using only css ?

类似:

.item:hover .wrapper { /*some css*/ }

只有 'wrapper' 不在 'item' 内,它在其他地方.

Only 'wrapper' is not inside 'item', it's somewhere else.

我真的不想在这么简单的事情上使用 javascript,但如果我必须这样做,我该怎么做?这是我失败的尝试:

I really don't want to use javascript for something this simple, but if I have to, how would I do it ? Here's my failed attempt:

document.getElementsByClassName('item')[0].onmouseover="document.getElementsByClassName('wrapper')[0].style.background="url('some url')";";

每个类只有一个元素.不知道为什么他们在制作模板时不使用 ID,但就是这样,我无法更改.

There's only one element of each class. Don't know why they didn't use IDs when they made the template, but that's just how it is and I can't change it.

这是一个菜单.每个菜单元素都有一个不同的类.当您将鼠标悬停在元素上时,右侧会弹出一个子菜单.这就像一个叠加层,当我使用检查元素"工具时,我可以看到当子菜单处于活动状态时整个网站的 html 都发生了变化(这意味着除了子菜单什么都没有).我称之为包装器"的类具有控制子菜单背景的 css.我真的看不出这两个类之间有什么联系.

It's a menu. Each menu element has a distinct class. When you hover on the element a submenu pops up to the right. It's like an overlay, when I use the 'Inspect Element' tool I can see that the whole website html changes when the submenu is active(meaning there's nothing but the submenu). The class I call 'wrapper' has the css that controls the background for the submenu. There's really no connection that I can see between the two classes.

推荐答案

目前在 CSS 中是不可能的,除非你想选择一个子元素或兄弟元素(微不足道,在此处的其他答案中有描述).

It's not possible in CSS at the moment, unless you want to select a child or sibling element (trivial and described in other answers here).

对于所有其他情况,您将需要 JavaScript.jQuery 和 Angular 等框架可以相对轻松地解决这个问题.

For all other cases you'll need JavaScript. jQuery and frameworks like Angular can tackle this problem with relative ease.

使用新的 CSS (4) 选择器 :has(),您可以将能够定位父元素/类,在不久的将来使纯 CSS 解决方案可行!

With the new CSS (4) selector :has(), you'll be able to target parent elements/classes, making a CSS-Only solution viable in the near future!

这篇关于使用 :hover 修改另一个类的css?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

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 tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493

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