将鼠标悬停在另一个元素上更改元素样式
我有三个元素,我需要通过将鼠标悬停在其他两个元素上来更改一个元素的样式。
html
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<div class='not-current'><a href="?page={{ page_obj.previous_page_number }}">{{ page_obj.previous_page_number }}</a></div>
{% endif %}
<div id='current'>
{{ page_obj.number }}
</div>
{% if page_obj.has_next %}
<div class='not-current' ><a href="?page={{ page_obj.next_page_number }}">{{ page_obj.next_page_number }}</a></div>
{% endif %}
</span>
</div>
css
#current, .not-current:hover {
width: 30px;
height: 30px;
line-height: 30px;
font-size: 20px;
font-weight: bold;
border: orange outset 3px;
background-color: orange;
margin: 0 auto 10px auto;
box-shadow: 1px 1px 3px 0 rgba(0, 0, 0, .5);
}
.not-current {
width: 15px;
height: 15px;
line-height: 15px;
background-color: #A29F9F;
border: #A29F9F outset 2px;
box-shadow: 1px 1px 3px 0 rgba(0, 0, 0, .5);
}
.not-current:hover #current {
display: none;
}
悬停元素(.not-current)的样式已更改,但 #current 元素的样式未更改。我哪里错了? (仅在 Chromium 12.0 中测试)。
I have a three elements and I need to change style of one element by hover on other two.
html
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<div class='not-current'><a href="?page={{ page_obj.previous_page_number }}">{{ page_obj.previous_page_number }}</a></div>
{% endif %}
<div id='current'>
{{ page_obj.number }}
</div>
{% if page_obj.has_next %}
<div class='not-current' ><a href="?page={{ page_obj.next_page_number }}">{{ page_obj.next_page_number }}</a></div>
{% endif %}
</span>
</div>
css
#current, .not-current:hover {
width: 30px;
height: 30px;
line-height: 30px;
font-size: 20px;
font-weight: bold;
border: orange outset 3px;
background-color: orange;
margin: 0 auto 10px auto;
box-shadow: 1px 1px 3px 0 rgba(0, 0, 0, .5);
}
.not-current {
width: 15px;
height: 15px;
line-height: 15px;
background-color: #A29F9F;
border: #A29F9F outset 2px;
box-shadow: 1px 1px 3px 0 rgba(0, 0, 0, .5);
}
.not-current:hover #current {
display: none;
}
Styles of hovered element (.not-current) are changed but styles of #current element aren't changed. Where am I wrong in there? (Tested only in Chromium 12.0).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 CSS 同级选择器
~
来完成此操作,但需要注意的是,您的悬停元素必须位于要在标记中设置样式的元素之前。不过,您可以按照您喜欢的任何顺序显示它们。演示:
CSS:
HTML:
You can do this with the CSS sibling selector
~
, with the caveat that your hover element must come before the elements you want to style in the markup. You can display them in any order you like, though.Demo:
CSS:
HTML:
这是因为
#current
不在.not-current
下。使用 JavaScript 可以更好地实现此行为。That's because
#current
is not under.not-current
. This behaviour is better implemented using JavaScript..not-current:hover #current
表明#current
位于.not-current
下,正如 @silverstrike 所说。在这个上使用 JavaScript: http://www.w3schools.com/js/js_events.asp< /a>
或
jQuery:http://api.jquery.com/mouseover/
.not-current:hover #current
states that#current
is under.not-current
as @silverstrike said.Go with JavaScript on this one: http://www.w3schools.com/js/js_events.asp
or
jQuery: http://api.jquery.com/mouseover/
最好的办法是使用 :hover 伪类定位父级,并使用 :not(:hover) 为子级设置样式,如图所示,
您的 css 将如下所示
the best thing to do is to target the parent with the :hover pseudo-class and style the child using :not(:hover) as shown
your css will look like this