什么是使用CSS或JavaScript具有相同ID的多个DIV的悬停效应的最简单方法?
悬停,我想要不透明度=“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用
:悬停
CSS选择器添加样式Just use
:hover
css selector to add the styles使用类(.box)而不是使用ID(#box)。 Web可访问性指南不允许多个ID。
https://www.org/www.org/tr/wcag20-techs/h93。 html
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
在
onmouse*
事件中使用此:Use
this
in theonmouse*
events: