简体   繁体   English

如何在孩子ul悬停时更改父母li的颜色

[英]How to change parent li color on child ul hover

I have a menu structured in the following way, and I'm trying to give the previous li a background change when hovering on the nested li. 我有一个按以下方式构造的菜单,并且在悬停在嵌套的li上时,我试图对前一个li进行背景更改。

<ul class="level0">
   <li class="level1">Nav Item</li>
   <li class="level1">Nav Item</li>
   <ul class="level1">
      <li class="level2">Nav Item</li>
      <li class="level2">Nav Item</li>
      <li class="level2">Nav Item</li>
   </ul>
   <li class="level1">Nav Item</li>
   <li class="level1">Nav Item</li>
</ul>

On ul.level1:hover highlight li.level1 在ul.level1:悬停时突出显示li.level1

Is this possible with CSS/SASS alone? 仅使用CSS / SASS是否有可能?

Edit: Example Added http://codepen.io/curiouscrusher/pen/avpEbZ?editors=110 编辑:示例已添加http://codepen.io/curiouscrusher/pen/avpEbZ?editors=110

You could add a 'parent' class to the parent, and then add a rule for the child, specifically for its parent, not super efficient though: 您可以向父级添加一个“父级”类,然后为子级(特别是为其父级)添加一条规则,但并不是超级有效:

 nav { .parent{ color:black; } ul:hover{ .parent{ color:red; } } li { padding: .5em 0; &:hover { cursor: pointer; font-weight: bold; } } } 
 <nav> <ul class="level0"> <li class="level1">Nav Item</li> <li class="level1 parent">Make This li Change</li> <ul class="level1"> <li class="level2">When Hovering Here</li> <li class="level2">Nav Item</li> <li class="level2">Nav Item</li> </ul> <li class="level1">Nav Item</li> <li class="level1">Nav Item</li> </ul> </nav> 

here is a sample same as your problem. 这是与您的问题相同的示例。 visit here and change your code. 访问此处并更改您的代码。

DEMO DEMO

 nav ul.level0 li{ color: #000000; } nav ul.level0 ul.level1 li.level2{ color: #78AB46; } nav ul.level0 li.level1 span { color: #000000; } nav ul.level0 ul.level1 .level2:hover,nav ul.level0 ul.level1:hover> li:first-child,nav ul.level0 ul.level1 .level2:hover>li.level1 span { color:blue; } 
  <nav> <ul class="level0"> <li class="level1">Nav Item</li> <ul class="level1"> <li class="level2">When Hovering Here</li> <li class="level2">Nav Item</li> <li class="level2">Nav Item</li> </ul> <li class="level1">Nav Item</li> <li class="level1">Nav Item</li> </ul> </nav> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM