CSS 悬停一个元素,然后更改另一个元素(不使用 Javascript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7832603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:57:08  来源:igfitidea点击:

Hover one element, and change another (without using Javascript)

csshover

提问by Arlington

I have a nested CSS menu that I can't get the submenus to come up.

我有一个嵌套的 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.

我把A list 中的代码分开了。该站点上的示例运行良好,但由于我的页面上有 2 个 CSS 导航菜单,因此我必须将 HTML 元素放在不同的 CSS 类中。

Here is my code:

这是我的代码:

<!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>

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!

现在,当我将鼠标悬停在第 1 级的“a”上时,第 2 级的“ul”不会出现。有人可以透露一些信息吗?我可能遗漏了一些明显的东西。谢谢!

回答by Cristian Necula

You must change your CSS selector to target the lvl2 <ul>, since it is not nested anymore (it's a sibling, so use +).

您必须更改 CSS 选择器以定位 lvl2 <ul>,因为它不再嵌套(它是兄弟,所以使用 +)。

a.lvl1:hover + ul.lvl2 {display: block} /* The magic */

You should read this list of css selectors.

您应该阅读此 css 选择器列表

Or you could move the hover on the lvl1 <li>, instead of the anchor

或者您可以将悬停移动到 lvl1 上<li>,而不是锚点上

li.lvl1:hover ul.lvl2 {display: block} /* The magic */