【问题标题】:How to have multiple dropdown buttons in a navbar?如何在导航栏中有多个下拉按钮?
【发布时间】:2019-10-23 22:30:18
【问题描述】:

我正在构建一个新网站,并希望拥有可移动访问的下拉菜单。我没有使用悬停下拉菜单,而是尝试构建可点击的下拉菜单。

我已经尝试查看其他人做了什么,但到目前为止还没有找到解决这个问题的人,尽管我认为这对大多数网站建设者来说相当容易。关于 HTML / CSS,有很多基本的东西我还不了解。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  cursor: pointer;
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.show {
  display: block;
}

.wrapper {
  width: 800px;
  margin: auto;
}
</style>
</head>
<body>

<div class="wrapper">
<div class="navbar">

  <a href="#home">Info</a>

  <div class="dropdown">
  <button class="dropbtn" onclick="myFunction()">Our methods
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-content" id="myDropdown">
    <a href="#link">Answer 1</a>
    <a href="#link">Answer 2</a>
    <a href="#link">Answer 3</a>
  </div>
  </div>

  <div class="dropdown">
  <button class="dropbtn" onclick="myFunction()">Products
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-content" id="myDropdown">
    <a href="#link">This is the first option</a>
    <a href="#link">This is the second option</a>
  </div>
  </div>

</div>
</div>

<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
  if (!e.target.matches('.dropbtn')) {
  var myDropdown = document.getElementById("myDropdown");
    if (myDropdown.classList.contains('show')) {
      myDropdown.classList.remove('show');
    }
  }
}

</script>
</body>
</html>

我们的目标是拥有一个导航栏,该导航栏有多个按钮,点击后会产生一个下拉菜单。相反,无论我点击哪个,我总是会打开第一个(在本例中带有“答案”而不是“选项”的那个)。

我怎样才能让每个人都打开自己的下拉菜单?

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您遇到的情况是由于两个下拉 div 中的“id”属性相同。尽管浏览器允许这样做,但在 HTML 中 id 应该是唯一的(正是针对这种情况)。

    不管有其他更好的方法来实现这一点,一个快速解决您的情况的方法是:

    • 分别为第一个和第二个下拉菜单设置id="myDropdown1"id="myDropdown2",以避免重复。
    • 将第一个和第二个按钮中的 onclick 属性分别更改为 onclick="myFunction(1)"onclick="myFunction(2)"
    • 更改 myFunction 以考虑该数字,例如
        function myFunction(num) {
          document.getElementById("myDropdown" + num).classList.toggle("show");
        }
    

    祝你好运!

    【讨论】:

    • 这个建议使它完全按照它应该的方式工作。谢谢!包括关于 id 的建议。
    • 这可能超出了问题的范围,但是当打开一个新的时,有没有一种简单的方法可以隐藏其他的?
    • @MirjamHeijn 例如,myFunction 开头的类似内容? document.getElementsByClassName('dropdown-content').forEach(function(dropdown) { dropdown.classList.remove('show'); });
    • @jaoquin gatica 谢谢,不幸的是,下拉菜单再也不会出现了。我也意识到我有点破坏了关闭窗口的最后一段代码,因为它专注于特定的一个。至少现在我知道要修复和处理哪个部分。谢谢。
    • 对于遇到相同问题的人,我的另一个问题的解决方案在这里:stackoverflow.com/questions/58582128/…
    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 2020-12-20
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2017-10-21
    • 2011-08-09
    • 1970-01-01
    相关资源
    最近更新 更多