一次改变两个div的背景颜色
所以我需要将一个 div 设为一个链接,并在用鼠标悬停在该 div 上时背景颜色发生变化。问题是,这个 div 里面有两个子 div,当我将鼠标移到父 div 的边界时,它实际上位于子 div 上。因此,虽然我可以使这些子 div 之一在悬停时发生变化,但第二个不会发生变化。
所以我想我的问题是,有没有一种方法可以让两个子 div 在使用 css 悬停时发生变化?
如果更容易的话,我不介意更改代码以使用表格,但我需要找到某种方法来使整个 div / tr 在悬停在一个孩子 / td 上时发生变化。
我实际上想要在这里创建的是与 youtube 推荐的视频框(在页面右侧)几乎相同的东西,
提前感谢
CSS
#parent {
width: 318px;
height: 90px;
background-color: #FFFFFF;
margin: 5px 0px 5px 0px;
font-size: 10px;
}
#parent :hover {
background-color: #0000ff;
}
#child1 {
width:120px;
float:left;
}
child2 {
width:188px;
float:right;
}
HTML(以及其他一些东西)
<c:forEach var="item" items="${list}">
<a href="webpage?item.getinfo()">
<div id="parent">
<div id="child1">
<img src="img.jpg">
</div>
<div id="child2">
${item.getinfo2()} <br>
${item.getinfo3()} <br>
</div>
</div>
</a>
</c:forEach>
代码是类似的东西。我最近一段时间一直在破解它,但这就像我以前遇到的那样
So i need to make a div a link, and have the background colour change when hoverng over this div with the mouse. The problem is, this div has two child divs inside it and when i move the mouse in to the bounds pf the parent div it is actually on a child div. So while i can make it so that one of these child divs changes on hover the second one does not.
So i guess my question is, is there a way to make both child divs change when hovering one using css?
I dont mind changing code to use tables if thats easier but I need to find some way to make the entire div / tr change when hovering on one child / td.
What im actually looking to create here is something almost the same as th youtube recommended videos boxes (on teh right of the page)
Thanks in advance
CSS
#parent {
width: 318px;
height: 90px;
background-color: #FFFFFF;
margin: 5px 0px 5px 0px;
font-size: 10px;
}
#parent :hover {
background-color: #0000ff;
}
#child1 {
width:120px;
float:left;
}
child2 {
width:188px;
float:right;
}
HTML (with some other stuff)
<c:forEach var="item" items="${list}">
<a href="webpage?item.getinfo()">
<div id="parent">
<div id="child1">
<img src="img.jpg">
</div>
<div id="child2">
${item.getinfo2()} <br>
${item.getinfo3()} <br>
</div>
</div>
</a>
</c:forEach>
Code is something like that. Ive been hacking it up for the last while but that was something like what i had before
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您能够将鼠标悬停在第一个上,则只需要 CSS:
使用此 HTML:
将鼠标悬停在 Child 1 上也会突出显示 Child 2。反之亦然,但在 CSS 中不起作用,因此需要一些 JS 。
If the one you're able to hover over is the first, you only need CSS:
With this HTML:
Hovering over Child 1 will also highlight Child 2. Vice-versa doesn't work in CSS though, so that would need some JS.
我认为你可能只需要修复 CSS 的一行。更改:
至:
这似乎对我有用。
I think you might just need to fix a line of your CSS. Change:
to:
That seemed to work for me.
您尝试过使用 jQuery 吗?你可以这样做:
http://jsfiddle.net/UtdYY/
Html:
Javascript:
CSS:
< strong>编辑:清理了 javascript,谢谢 elclanrs
Have you tried using jQuery? You could do something like this:
http://jsfiddle.net/UtdYY/
Html:
Javascript:
CSS:
Edit: Cleaned up the javascript, thanks elclanrs
试试这个http://jsfiddle.net/rsarika/rtGw5/1/
Try this http://jsfiddle.net/rsarika/rtGw5/1/