[英]How to hover on child <li> elements
Please look at the code snippet below. 请查看下面的代码段。
The structure is 结构是
<ul>
<li>text1</li>
<li> <ul><li> text2 </li>
<li> text3 </li>
</ul>
</li>
</ul>
I have the CSS as follows: 我的CSS如下:
li:hover
{
background-color: yellow;
}
It works fine for the first li
but when I hover over the second item (in the sub-ul), then all items in the sub list are highlighted. 它对第一个
li
很好用,但是当我将鼠标悬停在第二个项目上时(在子ul中),子列表中的所有项目都将突出显示。
What I need is for a single row to be highlighted at a time, regardless of the relationship of the list item. 我需要的是一次突出显示一行,而不管列表项的关系如何。
I tried 我试过了
li:child-only:hover
but it did not work. 但它没有用。 All other answers on SO are approaching jQuery to handle this issue.
关于SO的所有其他答案都在使jQuery处理此问题。 Can this be solved using only CSS?
可以仅使用CSS来解决吗?
ul { list-style-type: none; } li:hover { color: blue; cursor: pointer; background-color: yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>Lv1: First Item</li> <li>Lv1: Second Item</li> <li> <ul> <li>Lv2: First Item</li> <li> <ul> <li>Lv3: First Item</li> <li>Lv3: Second Item</li> <li>Lv3: Third Item</li> </ul> </li> <li>Lv2: Second Item</li> <li>Lv2: Third Item</li> </ul> </li> </ul>
Just change the styles set for li inside ul as you want them to be: 只需更改ul内部为li设置的样式,即可:
ul { list-style-type: none; color: black; cursor: default; background-color: white; } li:hover { color: blue; cursor: pointer; background-color: yellow; }
<ul> <li>Lv1: First Item</li> <li>Lv1: Second Item</li> <li> <ul> <li>Lv2: First Item</li> <li> <ul> <li>Lv3: First Item</li> <li>Lv3: Second Item</li> <li>Lv3: Third Item</li> </ul> </li> <li>Lv2: Second Item</li> <li>Lv2: Third Item</li> </ul> </li> </ul>
You should put the :hover
property on an element surrounding the text content of the li
, like so: 您应该将
:hover
属性放在围绕li
的文本内容的元素上,如下所示:
ul { list-style-type: none; } li span:hover { color: blue; cursor: pointer; background-color: yellow; }
<ul> <li><span>Lv1: First Item</span></li> <li><span>Lv1: Second Item</span></li> <li> <ul> <li><span>Lv2: First Item</span></li> <li> <ul> <li><span>Lv3: First Item</span></li> <li><span>Lv3: Second Item</span></li> <li><span>Lv3: Third Item</span></li> </ul> </li> <li><span>Lv2: Second Item</span></li> <li><span>Lv2: Third Item</span></li> </ul> </li> </ul>
(or maybe use div
s if you want to highlight a full row of content) (或者如果要突出显示整行内容,则可以使用
div
)
ul { list-style-type: none; } ul li:hover { color: blue; cursor: pointer; background-color: yellow; } ul li:hover ul{ background-color: white; color: black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>Lv1: First Item</li> <li>Lv1: Second Item</li> <li> <ul> <li>Lv2: First Item</li> <li> <ul> <li>Lv3: First Item</li> <li>Lv3: Second Item</li> <li>Lv3: Third Item</li> </ul> </li> <li>Lv2: Second Item</li> <li>Lv2: Third Item</li> </ul> </li> </ul>
U have to reset for other while hovering. U悬停时必须重置其他位置。
You can override the background color of sub-ul's with the page background color: 您可以使用页面背景颜色覆盖sub-ul的背景颜色:
ul > li:hover {
background: yellow;
}
ul > li:hover > ul {
background: white;
}
Example: http://jsfiddle.net/xo1g3sub/ 示例: http : //jsfiddle.net/xo1g3sub/
I added few aditional classes so you can actually target the elements you want to style. 我添加了几个附加类,因此您实际上可以定位要设置样式的元素。 Here you can find more info about
>
Is there a CSS selector for the first direct child only? 在这里,您可以找到有关
>
更多信息>
仅第一个直接孩子有CSS选择器吗?
ul { list-style-type: none; } .first > li:hover { color: blue; cursor: pointer; background-color: yellow; } .first > .nested:hover{ background:none; color:black; }
<ul class="first"> <li>Lv1: First Item</li> <li>Lv1: Second Item</li> <li class="nested"> <ul> <li>Lv2: First Item</li> <li> <ul> <li>Lv3: First Item</li> <li>Lv3: Second Item</li> <li>Lv3: Third Item</li> </ul> </li> <li>Lv2: Second Item</li> <li>Lv2: Third Item</li> </ul> </li> </ul>
If you are looking for a very elegant event
driven solution, you should implement below code: 如果您正在寻找一种非常优雅的
event
驱动解决方案,则应实现以下代码:
Note: It uses
e.stopPropagation()
to execute event only on hovered element.注意:它使用
e.stopPropagation()
仅在悬停的元素上执行事件。 And this way you add only 2 event listeners that take care of the entire list.这样,您仅添加2个事件侦听器即可处理整个列表。 No
HTML
orCSS
modifications are necessary.无需修改
HTML
或CSS
。
$('ul').delegate('li', 'mouseover', function (e) {
if (e.currentTarget.tagName == 'LI') {
$(e.currentTarget).css('background-color','yellow');
e.stopPropagation();
}
});
$('ul').delegate('li', 'mouseout', function (e) {
if (e.currentTarget.tagName == 'LI') {
$(e.currentTarget).css('background-color','');
e.stopPropagation();
}
});
Working fiddle: https://jsfiddle.net/urahara/t6fp2un2/ 工作提琴: https : //jsfiddle.net/urahara/t6fp2un2/
<ul>
<li>Lv1: First Item</li>
<li>Lv1: Second Item</li>
<li>Lv1: Third Item
<ul>
<li>Lv2: First Item</li>
<li>Lv2: Second Item
<ul>
<li>Lv3: First Item</li>
<li>Lv3: Second Item</li>
<li>Lv3: Third Item</li>
</ul>
</li>
<li>Lv2: Second Item</li>
<li>Lv2: Third Item</li>
</ul>
</li>
</ul>
ul {
list-style-type: none;
position: relative;
}
ul li:hover {
color: blue;
cursor: pointer;
background-color: yellow;
}
ul > li{
display:inline-block;
}
ul li > ul{
display:none;
}
ul li > ul li{
display:block;
color:#fff;
}
ul li:hover > ul{
display: block;
position: absolute;
border: 1px solid #000;
background:blue;
}
ul li ul{
position: relative;
}
ul li ul li ul{
position: absolute;
border: 1px solid #000;
left: 100%;
}
ul li ul li:hover > ul {
display: block;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.