简体   繁体   English

css悬停元素来显示孩子,当我将鼠标悬停在孩子身上时,如何将悬停css保留在父母身上?

[英]css hover element to show child, how can I keep the hover css on the parent when I mouseover the child?

I have a <ul> with several <li> elements. 我有一个带有几个<li>元素的<ul> Inside the <li> element there is a large <div> that is hidden by default. <li>元素内部有一个默认隐藏的大<div> When I hover the <li> its background changes color as does the bottom border as well as displaying the <div> . 当我悬停<li>它的背景会像底部边框一样改变颜色,也会显示<div> This is the expected behavior. 这是预期的行为。 Note I am using css and not javascript for the hover effect. 注意我使用css而不是javascript来悬停效果。 .menuItem:hover .menuBody { display:block;} . .menuItem:hover .menuBody { display:block;} My problem is that when I hover over the now showing <div> its parent, the <li> in this case, loses it's hover style. 我的问题是,当我将鼠标悬停在现在显示的<div>其父级时,在这种情况下<li>会丢失它的悬停样式。 How can I force the parent to keep the hover styling when I hover over the <div> ? 当我将鼠标悬停在<div>时,如何强制父母保留悬停样式? Basic html for the menu is like this. 菜单的基本html是这样的。 Any help or tips is of course appreciated. 任何帮助或提示当然是值得赞赏的。

<ul>
    <li class="menuItem">
        <a href="#">MenuItem</a>
        <div class="menuBody></div>
    </li>
</ul>

What you are asking is actually default behavior. 你问的实际上是默认行为。 When you hover over a child, the parent also receives the hover status, and the hover css should still be displayed. 当您将鼠标悬停在子项上时,父项也会收到悬停状态,并且仍应显示悬停css。 Have a look at this example: 看看这个例子:

http://jsfiddle.net/jxZ6S/ http://jsfiddle.net/jxZ6S/

We will need some more code to see what exactly is wrong. 我们需要更多代码来查看究竟是什么问题。 Are you sure you are applying the hover style to the parent, and not to the a ? 您确定要将悬停样式应用于父级,而不是a吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CSS,当我将鼠标悬停在父元素上时,可以更改子元素的属性吗? - With CSS, can I change the property of a child element when I hover on a parent element? 将鼠标悬停在子元素CSS上时,保持父对象处于活动状态 - Keep parent active when hover on the child element CSS 在 CSS 中,在父元素悬停时显示子元素 - Show child element on parent hover in CSS 当将鼠标悬停在其子元素上时,如何保持父元素的背景颜色不变? - how can I keep parent element's background-color unchanged when hover on its child element? 当鼠标移动到子元素时,如何保持CSS悬停规则适用? - How do I keep a CSS hover rule applied when the mouse moves to the child element? 将鼠标悬停在子元素上时更改父元素的CSS属性 - Change css properties of parent element when hover on child element 如何将鼠标悬停在子元素上而不将悬停在CSS中的父元素上? - How do I hover over a child element without hovering over the parent in CSS? 当我将鼠标悬停在父母身上时,如何切换孩子? - How can I toggle a child when I hover over a parent? 父级处于悬停状态时CSS转换子级 - CSS transitioning child when parent is in hover state CSS 效果 :hover::after 在子元素上 :hover 在父元素上 - CSS effect :hover::after on child element on :hover on parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM