简体   繁体   English

在孩子的悬停上 <ul><li> 父元素的元素更改样式 <ul><li> 元件

[英]On hover of child <ul> <li> element change styles of parent <ul><li> element

no jquery please 请不要使用jQuery

I have the following menu with class=menu-list, I want that on hover of nested/child ul li item, to change background color to white of parent li element 我有以下带有class = menu-list的菜单,我希望将其悬停在嵌套的/子ul li项目上,以将背景颜色更改为父li元素的白色

Code and working demo: 代码和工作演示:

  nav .menu-list{ background: white; position: absolute; margin-top: 60px; padding: 12px 0; display: none; width: 68vw; height: 100vh; } nav .menu-list li{ background: white; display: block; color: #000000; position: relative; padding: 0; border-left: 24px white solid; } nav .menu-list li:hover{ background: #eee; border-left: 24px #eee solid; } nav ul li:focus { background: white; } nav ul li:focus ul { display: block; position: relative; left: 0; margin-left: -24px; } nav ul li ul li:hover { background: white; } 
 <nav> <ul class="menu-list" style="display: block;"> <li tabindex="1" onclick="location.href='#/work'; dimScreen()">Work</li> <li tabindex="2" onclick="dimScreen()">About <ul> <li onclick="location.href='#/about/what-we-do'">What we do</li> <li onclick="location.href='#/about/how-we-work'">How we work</li> <li onclick="location.href='#/about/leadership'">Leadership</li> </ul> </li> </ul> </nav> 

Lame paint screenshot to explain better: me脚的油漆截图更好地解释了: 在此处输入图片说明

First thing, you can't do this with CSS . 首先,您不能使用CSS做到这一点 CSS has no concept of what it's parent element is. CSS没有关于其父元素是什么的概念。

As for javascript, you can use mouse events. 至于javascript,您可以使用鼠标事件。 Here's an example : 这是一个例子

var parentOn = function(e) {
    e.target.parentNode.style.backgroundColor = '#9d7';
}
var parentOff = function(e) {
    e.target.parentNode.style.backgroundColor = '#97a';
}

var children = document.querySelectorAll('.child');    
for (var x = 0; x < children.length; x ++) {
    children[x].addEventListener('mouseover', parentOn);
    children[x].addEventListener('mouseout', parentOff);
};

A few things to note: 注意事项:

  • Any JavaScript DOM query will return an array of nodes, so you must iterate over them to avoid error. 任何JavaScript DOM查询都将返回节点数组,因此您必须对其进行迭代以避免错误。 JQuery hides this nicely so it can be confusing. jQuery将其很好地隐藏起来,因此可能会造成混淆。

  • the mouseenter and mouseleave combination is subtley different from mouseover and mouseout, you can find out more here mouseenter和mouseleave组合与mouseover和mouseout的巧妙区别在于,您可以在此处找到更多信息

  • Working out how to use JavaScript to add and remove classes would be better than using inline styles. 弄清楚如何使用JavaScript添加和删除类比使用内联样式更好。 This answer will help . 这个答案会有所帮助 You'll be able to re-use the styles and it'll be easier to maintain - I'd such creating a utility function to handle this, like JQuery's addClass() . 您将能够重复使用样式,并且维护起来会更容易-我希望创建一个实用程序函数来处理此问题,例如JQuery的addClass()

  • Using named function variables is a bit clearer than anonymous functions, and more re-usable. 使用命名函数变量比匿名函数更清晰,并且更可重用。

https://jsfiddle.net/chwbuvum/6/ https://jsfiddle.net/chwbuvum/6/

Here's a pure CSS solution 这是一个纯CSS解决方案

As Tony Leigh pointed out, you cannot do exactly what you asked using pure CSS, but you can make it look like you want. 正如Tony Leigh指出的那样,您不能完全使用纯CSS来完成您所要求的操作,但是可以使它看起来像您想要的。

The changed lines in the CSS are commented in the snippet, and the main change in the HTML is the addition of the span tag in the top-level list elements. 片段中注释了CSS中更改的行,HTML的主要更改是在顶级列表元素中添加了span标签。

The main idea is that each top-level li remains white, while the enclosed span turns gray on hover. 主要思想是每个顶级li保持白色,而封闭的span在悬停时变为灰色。 That way, hovering over a sub-list doesn't change color of the parent li . 这样,将鼠标悬停在子列表上不会更改父li颜色。

Code Snippet 代码段

  nav .menu-list{ background: white; position: absolute; margin-top: 60px; padding: 12px 0; display: none; width: 68vw; height: 100vh; } nav .menu-list li{ background: white; display: block; color: #000000; position: relative; padding: 0; /* changed from border to background color border-left: 24px white solid; */ padding-left:24px; /* added to put back the "border-left" area */ } nav .menu-list li:hover{ background: white; /*changed from #eee to white */ /* changed from border to background color border-left: 24px #eee solid; */ } /* start added blocks */ nav .menu-list li span { width: 100%; display:block; position:relative; left:-24px; padding-left:24px; } nav .menu-list li span:hover{ background: #eee; } /* end added blocks */ nav ul li:focus { background: white; } nav ul li:focus ul { display: block; position: relative; left: 0; margin-left: -24px; } nav .menu-list li ul li:hover { background: #eee; } 
 <nav> <ul class="menu-list" style="display: block;"> <li tabindex="1" onclick="location.href='#/work'; dimScreen()"><span>Work</span></li> <li tabindex="2" onclick="dimScreen()"><span>About</span> <ul> <li onclick="location.href='#/about/what-we-do'">What we do</li> <li onclick="location.href='#/about/how-we-work'">How we work</li> <li onclick="location.href='#/about/leadership'">Leadership</li> </ul> </li> </ul> </nav> 

Here's a code based solution: 这是一个基于代码的解决方案:

JS JS

var listElements = document.getElementsByClassName('submenu');
for (var i = 0; i < listElements.length; i++) {
    listElements[i].addEventListener("mouseenter", function(evt) {   
        evt.target.parentNode.style.background = "red";
    });
    listElements[i].addEventListener("mouseleave", function(evt) {   
        evt.target.parentNode.style.background = "black";
    });
}

HTML CHANGE HTML更改

<ul class="menu-list" style="display: block;">
  <li tabindex="1" onclick="location.href='#/work'; dimScreen()">Work</li>
  <li tabindex="2" onclick="dimScreen()">About
    <ul class='submenu'>
      <li onclick="location.href='#/about/what-we-do'">What we do</li>
      <li onclick="location.href='#/about/how-we-work'">How we work</li>
      <li onclick="location.href='#/about/leadership'">Leadership</li>
    </ul>
  </li>
</ul>

As you can see, for each of your submenus you will have, I added an additional submenu class to differentiate between the main menu and submenu. 如您所见,对于您的每个子菜单,我添加了一个附加的submenu类,以区分主菜单和子菜单。 When you leave each element, it will change the background of the parent to black. 当您离开每个元素时,它将父级的背景更改为黑色。 When you enter, it will change it to red, based on the mouseenter and mouseleave events. 输入时,它将根据mouseentermouseleave事件将其更改为红色。

What it does is find the parent node and then manipulate the styling of that element. 它的作用是找到父节点,然后操纵该元素的样式。

Thanks everyone for trying but I only needed to add this to make it work 感谢大家的尝试,但我只需要添加此内容即可使其正常工作

  .menu-list li:hover {
        background: #eee;
    }

    .menu-list li:focus {
        background: white;
    }

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

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