什么是使用CSS或JavaScript具有相同ID的多个DIV的悬停效应的最简单方法?

发布于 2025-01-23 16:57:39 字数 1342 浏览 4 评论 0原文

悬停,我想要不透明度=“ 0.3”; 不悬停,我想要不透明度=“ 1”; 每种div颜色中的H2和H3从白色到红色。

我在各处搜索过谷歌搜索,并试图弄清楚它,但是菜鸟是菜鸟。 这是我尝试的方法,但仅适用于第一个Div。为什么其余的工作不能工作?

我知道如果我使用课程,我可以使用CSS进行,但是我想在每个Div中应用更多功能。例如,当悬停在框上时,H2和H3显示器从无块。或颜色从白色到红色。

这是链接 笔上的代码

这是代码

html

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
 
 
</body>
</html>

css

#box{
    background: blue;
    width:100px;
    height: 100px;
    margin: 20px;
  }

js

  function hover() {
document.getElementById("box").style.opacity = "0.3";
     }
 
function nohover() {
document.getElementById("box").style.opacity = "1";
     }

hover, I want the opacity = "0.3";
no hover, I want the opacity = "1";
and h2 and h3 in each div color goes from white to red.

I've googled everywhere and tried to figure it out, but a noob is a noob.
here is what I tried but it works for the first div only. why can't work for the rest?

I know if I use class, I can do it with CSS, but I want to apply more functions for h2 and h3 in each div. for example, when hover on box, h2 and h3 display goes from none to block.or color from white to red.

here is the link
the code on pen

here is the code

HTML

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
  <div id="box" onmouseover="hover()" onmouseout="nohover()"></div>
 
 
</body>
</html>

CSS

#box{
    background: blue;
    width:100px;
    height: 100px;
    margin: 20px;
  }

js

  function hover() {
document.getElementById("box").style.opacity = "0.3";
     }
 
function nohover() {
document.getElementById("box").style.opacity = "1";
     }

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

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

发布评论

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

评论(3

酷炫老祖宗 2025-01-30 16:57:39

只需使用:悬停 CSS选择器添加样式

#box{
    background: blue;
    width:100px;
    height: 100px;
    margin: 20px;
  }
  
#box:hover {
  opacity: 0.3;
}
  <div id="box" ></div>
  <div id="box" ></div>
  <div id="box" ></div>
  <div id="box" ></div>
 

Just use :hover css selector to add the styles

#box{
    background: blue;
    width:100px;
    height: 100px;
    margin: 20px;
  }
  
#box:hover {
  opacity: 0.3;
}
  <div id="box" ></div>
  <div id="box" ></div>
  <div id="box" ></div>
  <div id="box" ></div>
 

轻许诺言 2025-01-30 16:57:39

使用类(.box)而不是使用ID(#box)。 Web可访问性指南不允许多个ID。

https://www.org/www.org/tr/wcag20-techs/h93。 html

.box {
    background: blue;
}
.box:hover {
    background: red
 }

Use class (.box) instead of using id (#box). Multiple Ids is not allowed by web accessibility guidelines.

https://www.w3.org/TR/WCAG20-TECHS/H93.html

.box {
    background: blue;
}
.box:hover {
    background: red
 }
樱花落人离去 2025-01-30 16:57:39

onmouse*事件中使用此:

function hover(ev) {
  ev.style.opacity = "0.3";
}
 
function nohover(ev) {
  ev.style.opacity = "1";
}
<div id="box" onmouseover="hover(this)" onmouseout="nohover(this)"></div>

Use this in the onmouse* events:

function hover(ev) {
  ev.style.opacity = "0.3";
}
 
function nohover(ev) {
  ev.style.opacity = "1";
}
<div id="box" onmouseover="hover(this)" onmouseout="nohover(this)"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文