3个单独的下拉列表,仅选择前一个

问题描述 投票:-1回答:1

我创建了一个网站(我对在网站上使用html相当陌生...。我通常只使用图像和文本),我希望3个独立的下拉框可供选择,它们将进入3个不同的页面

问题是,所有列表都在编码中工作,但是当我单击任何按钮时,它只会选择顶部列表

3 drop down boxes

how the drop looks when you select any button

这是使用的编码ive。...对于每个下拉列表,有120个不同的类链接

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
  background-color: #005eb8;
  color: white;
  margin-left: 98px;
  padding: 5px;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #004a90;
}

.dropdown {
  position: absolute;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  margin-left: 98px;
  background-color: #f4f7f8;
  min-width: 50px;
  height: 440px;
  overflow-y: scroll;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 1px 6px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #005eb8; color:white;}

.show {display: block;}
</style>
</head>
<body>

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">--- Select ---</button>
  <div id="myDropdown1" class="dropdown-content">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20t.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91t.html">1890-91</a>

  </div>
</div>

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

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>

</body>
</html>

预先感谢

克里斯


更新


好。非常感谢您的帮助。我设法将3个不同的按钮转到3个不同的部分,但是最后一个问题是,单击页面上的其他任何位置时,顶部2个按钮不会关闭..但是底部按钮会关闭。这是代码。...有人可以建议关闭前两个脚本吗?网页为https://www.glossopnorthendafc.co.uk/history.html

<br>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn1 {
  background-color: #005eb8;
  color: white;
  margin-left: 86px;
  padding: 5px;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.dropbtn1:hover, .dropbtn1:focus {
  background-color: #004a90;
}

.dropdown1 {
  position: absolute;
  display: inline-block;
}

.dropdown-content1 {
  display: none;
  position: absolute;
  margin-left: 91px;
  background-color: #f4f7f8;
  min-width: 50px;
  height: 440px;
  overflow-y: scroll;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content1 a {
  color: black;
  padding: 1px 6px;
  text-decoration: none;
  display: block;
}

.dropdown1 a:hover {background-color: #005eb8; color:white;}

.show {display: block;}
</style>
</head>
<body>

<div class="dropdown1">
  <button onclick="myFunction1()" class="dropbtn1">--- Select ---</button>
  <div id="myDropdown1" class="dropdown-content1">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20r.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/2018-19r.html">2018-19</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91r.html">1890-91</a>

  </div>
</div>

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

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn1')) {
    var dropdowns = document.getElementsByClassName("dropdown-content1");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
 </script>

</body>
</html>
<br>

<br>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn2 {
  background-color: #005eb8;
  color: white;
  margin-left: 86px;
  padding: 5px;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.dropbtn2:hover, .dropbtn2:focus {
  background-color: #004a90;
}

.dropdown2 {
  position: absolute;
  display: inline-block;
}

.dropdown-content2 {
  display: none;
  position: absolute;
  margin-left: 91px;
  background-color: #f4f7f8;
  min-width: 50px;
  height: 440px;
  overflow-y: scroll;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content2 a {
  color: black;
  padding: 1px 6px;
  text-decoration: none;
  display: block;
}

.dropdown2 a:hover {background-color: #005eb8; color:white;}

.show {display: block;}
</style>
</head>
<body>

<div class="dropdown2">
  <button onclick="myFunction2()" class="dropbtn2">--- Select ---</button>
  <div id="myDropdown2" class="dropdown-content2">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20t.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/2018-19t.html">2018-19</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91t.html">1890-91</a>

  </div>
</div>

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

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn2')) {
    var dropdowns = document.getElementsByClassName("dropdown-content2");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>

</body>
</html>
<br>

<br>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn3 {
  background-color: #005eb8;
  color: white;
  margin-left: 86px;
  padding: 5px;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.dropbtn3:hover, .dropbtn3:focus {
  background-color: #004a90;
}

.dropdown3 {
  position: absolute;
  display: inline-block;
}

.dropdown-content3 {
  display: none;
  position: absolute;
  margin-left: 91px;
  background-color: #f4f7f8;
  min-width: 50px;
  height: 440px;
  overflow-y: scroll;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content3 a {
  color: black;
  padding: 1px 6px;
  text-decoration: none;
  display: block;
}

.dropdown3 a:hover {background-color: #005eb8; color:white;}

.show {display: block;}
</style>
</head>
<body>

<div class="dropdown3">
  <button onclick="myFunction3()" class="dropbtn3">--- Select ---</button>
  <div id="myDropdown3" class="dropdown-content3">
<a class="links" href="https://www.glossopnorthendafc.co.uk/2019-20s.html">2019-20</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/2018-19s.html">2018-19</a>
<a class=”links” href="https://www.glossopnorthendafc.co.uk/1890-91s.html">1890-91</a>

  </div>
</div>

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

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn3')) {
    var dropdowns = document.getElementsByClassName("dropdown-content3");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>

</body>
</html>
<br>
html dropdown
1个回答
0
投票

我在您的示例中仅看到一个<div class="dropdown">,但我认为您的代码中还有更多。如果是这种情况,请记住HTML ID是唯一的。尝试将myDropdown1更改为类,然后使用getElementsByClassName在JavaScript中选择。如果您需要直接帮助,请访问Twitter。上山人!

© www.soinside.com 2019 - 2024. All rights reserved.