悬停一个元素,并更改另一个(不使用Javascript) [英] Hover one element, and change another (without using Javascript)
问题描述
我有一个嵌套的CSS菜单,我不能得到子菜单。
I have a nested CSS menu that I can't get the submenus to come up.
我从列出。该网站的示例工作完美,但由于我有2个CSS导航菜单在我的页面上,我必须把我的HTML元素在不同的CSS类。
I took the code from A list apart. The example on that site works perfectly fine, but since I have 2 CSS navigational menus on my page, I have to put my HTML elements in different CSS classes.
这里是我的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
ul#lvl1 {
margin:0;
padding:0;
list-style:none;
width:150px; /* Width of Menu Items */
border-bottom:1px solid #ccc;
}
li.lvl1 {position:relative}
ul.lvl2 {
position: absolute;
left: 149px; /* Set 1px less than menu width */
top: 0;
display: none;
}
/* Styles for Menu Items */
li.lvl1 > a {
display: block;
text-decoration: none;
color: #777;
background: #fff; /* IE6 Bug */
padding: 5px;
border: 1px solid #ccc;
border-bottom: 0;
}
/* Fix IE. Hide from IE Mac \*/
* html.lvl1 > ul > li {float:left;height:1%}
* html.lvl1 > ul > li > a {height:1%}
/* End */
li.lvl2 > a:hover { color: #E2144A; background: #f9f9f9; } /* Hover Styles */
li.lvl2 > a { padding: 2px 5px; } /* Sub Menu Styles */
a.lvl1:hover ul.lvl2 {display: block} /* The magic */
</style>
</head>
<body>
<ul id="lvl1">
<li class="lvl1">
<a class="lvl1" href="#">item1</a>
<ul class="lvl2">
<li class="lvl2">
<a class="lvl2" href="#">subitem1</a>
</li>
</ul>
</li>
<li class="lvl1">
<a class="lvl1" href="#">item2</a>
<ul class="lvl2">
<li class="lvl2">
<a class="lvl2" href="#">subitem2</a>
</li>
</ul>
</li>
</ul>
</body>
</html>
现在当我将鼠标悬停在1级的a不会出现。有人可以请一些灯吗?我可能缺少一些明显的东西。感谢!
Now when I hover over the "a" on level 1, the "ul" on level 2 won't come up. Can someone please shed some light? I may be missing something obvious. Thanks!
推荐答案
您必须更改CSS选择器以定位lvl2 < ul& c $ c>,因为它不是嵌套的(它是一个兄弟,所以使用+)。
You must change your CSS selector to target the lvl2 <ul>
, since it is not nested anymore (it's a sibling, so use +).
a.lvl1:hover + ul.lvl2 {display: block} /* The magic */
$ b css选择器列表。
或者您可以移动lvl1 < li>
上的悬停而不是锚
Or you could move the hover on the lvl1 <li>
, instead of the anchor
li.lvl1:hover ul.lvl2 {display: block} /* The magic */
这篇关于悬停一个元素,并更改另一个(不使用Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!