【问题标题】:Hover not working in dropdown menu?悬停在下拉菜单中不起作用?
【发布时间】:2018-06-23 15:56:51
【问题描述】:

使用 glitch.com 并尝试创建一个简单的下拉菜单。 我使用了指南并尝试制作最简单的版本,这样我就不会遗漏任何东西。但是,我只是无法让悬停为我工作。

ul {
  padding: 0;
  margin: 0;
  position: absolute;
  list-style: none;
}

ul li {
  display: inline-block;
}

ul li a {
  height: 50px;
  min-width: 120px;
  line-height: 50px;
  background-color: black;
  text-decoration: none;
  text-align: center;
  display: block;
  color: #ffffff;
}

li ul {
  display: none;
}

li ul li {
  display: block;
  float: none;
}

ul li a:hover + .hidden, .hidden:hover {
  display:block;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Hello!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/styles.css">
    <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
    <script src="/script.js" defer></script>
  </head>
  <body>
    <ul>
      <li><a href="#">HOME</a></li>
      <li>
        <a href="#">ABOUT</a>
        <ul id="hidden">
          <li><a href="#">Team</a></li>
          <li><a href="#">Project</a></li>
          <li><a href="#">News</a></li>
        </ul>
      </li>
      <li><a href="#">CONTACT US</a></li>
    </ul>
  </body>
</html>

感觉很失败,不知道为什么它不起作用。 我已经搞砸了悬停语句只是为了让它工作但无济于事。

【问题讨论】:

  • 你在 CSS 中使用了id="hidden" =>,它必须是#hidden 而不是.hidden

标签: html css menu hover dropdown


【解决方案1】:

Css 将 ID 与 # 和 Class 与句点作为目标。

您已隐藏为 ID,因此您可以:

把你的css改成

ul li a:hover + #hidden, #hidden:hover {
  display:block;
}

或 html 到:

<ul class="hidden">

ul {
  padding: 0;
  margin: 0;
  position: absolute;
  list-style: none;
}

ul li {
  display: inline-block;
}

ul li a {
  height: 50px;
  min-width: 120px;
  line-height: 50px;
  background-color: black;
  text-decoration: none;
  text-align: center;
  display: block;
  color: #ffffff;
}

li ul {
  display: none;
}

li ul li {
  display: block;
  float: none;
}

ul li a:hover + #hidden, #hidden:hover {
  display:block;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Hello!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/styles.css">
    <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
    <script src="/script.js" defer></script>
  </head>
  <body>
    <ul>
      <li><a href="#">HOME</a></li>
      <li>
        <a href="#">ABOUT</a>
        <ul id="hidden">
          <li><a href="#">Team</a></li>
          <li><a href="#">Project</a></li>
          <li><a href="#">News</a></li>
        </ul>
      </li>
      <li><a href="#">CONTACT US</a></li>
    </ul>
  </body>
</html>

【讨论】:

  • 谢谢伙计,事情总是简单的。
  • @user3547778 没问题!祝您的编码之旅好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 2019-01-18
  • 2016-08-30
  • 1970-01-01
相关资源
最近更新 更多