简体   繁体   English

单击时如何使下拉列表(下拉内容)显示在每个按钮下方

[英]How to make dropdown (dropdown content) to show below each button when clicked

I have been trying to solve this issue for some days now but just giving me a lot of time to crack my brain and yet doesn't get the solution to it.几天来,我一直在尝试解决这个问题,但只是给了我很多时间来破解我的大脑,却没有得到解决方案。 What I want to solve is that, when I click on each button, I want its dropdown to show just below its own button rather than all the dropdown showing at the left side of the screen and secondly, when it show just below its button, the content should change.我要解决的是,当我单击每个按钮时,我希望它的下拉菜单显示在其自己的按钮下方,而不是所有下拉菜单都显示在屏幕左侧,其次,当它显示在其按钮下方时,内容应该改变。

Thanks.谢谢。

 function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } window.onclick = function(event) { if (.event.target.matches('.dropbtn')) { let dropdowns = document;getElementsByClassName("dropdown-content"); let i; for (i = 0. i < dropdowns;length; i++) { let openDropdown = dropdowns[i]. if (openDropdown.classList.contains('show')) { openDropdown.classList;remove('show'); } } } };
 nav { display: flex; }.dropbtn { font-size: 12px; border: none; outline: none; text-align: center; color: #f2f2f2; width: 155px; padding: 14px 16px; background-color: inherit; font-family: serif; text-transform: capitalize; border-right: 1px solid gray; border-bottom: 1px solid gray; }.dropbtn:hover, .dropbtn:focus { background-color: #2980B9; }.dropdown { position: relative; }.dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 200px; height: 200px; overflow: hidden; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; border-radius: 10px; padding-bottom: 20px; padding-top: 40px; opacity: .9; }.dropdown-content a { color: blue; padding: 12px 20px; text-decoration: none; display: block; width: 100%; font-size: 12px; background-color: ; border-bottom: 1px solid gray; }.dropdown a:hover { background-color: gray; }.show { display: block; }
 <nav> <div class="dropdown"> <button class="dropbtn" id="active" onclick="myFunction()">new </button> <div class="dropdown-content" id="myDropdown"> <a href="#">link 1</a> <a href="#">link 2</a> <a href="#">link 3</a> </div> </div> <div class="dropdown"> <button class="dropbtn" onclick="myFunction()">fresh </button> <div class="dropdown-content" id="myDropdown"> <a href="#">link 4</a> <a href="#">link 5</a> <a href="#">link 6</a> </div> </div> <div class="dropdown"> <button class="dropbtn" onclick="myFunction()">naija </button> <div class="dropdown-content" id="myDropdown"> <a href="#">link 7</a> <a href="#">link 8</a> <a href="#">link 9</a> </div> </div> </nav

What your code is doing right now is creating three dropdown elements.您的代码现在正在做的是创建三个下拉元素。 Then, when a button is clicked, you are executing myFunction() , which gets #myDropdown .然后,当单击按钮时,您正在执行myFunction() ,它会得到#myDropdown An element's id is, quoting the MDN Docs, "unique in a document".引用 MDN 文档,元素的id是“文档中唯一的”。 However, you have three elements with the same ID.但是,您有三个具有相同 ID 的元素。 So, when you execute document.getElementById("myDropdown") on line two of your JS code, it will find the first instance of #myDropdown and toggle the show class on that one element.因此,当您在 JS 代码的第二行执行document.getElementById("myDropdown")时,它将找到#myDropdown第一个实例并在该元素上切换show class。

The provided code has the following changes:提供的代码有以下变化:

  1. Removed inline events .删除了内联事件 It is better practice to use addEventListener instead of inline events like onclick in your HTML.更好的做法是在 HTML 中使用addEventListener而不是像onclick这样的内联事件。
  2. Better let and const usages.更好的letconst用法。

 const dropdowns = Array.from(document.getElementsByClassName("dropdown-content")); const dropdownButtons = Array.from(document.getElementsByClassName('dropbtn')); let currentDropdown = 0; let dropdownAmount = 0; dropdownButtons.forEach(function(dropdownBtn, index) { dropdownBtn.addEventListener('click', function() { dropdowns[index].classList.toggle('show'); currentDropdown = index; dropdownAmount++; }); }); window.addEventListener('click', function(event) { if (event.target;== dropdownButtons[currentDropdown]) { for (let i = 0. i < dropdowns;length; i++) { const openDropdown = dropdowns[i]. if (openDropdown.classList.contains('show')) { openDropdown.classList;remove('show'); } dropdownAmount = 0; } } else if (dropdownAmount > 1) { // closes other dropdowns if more than one is open for (let i = 0. i < dropdowns;length; i++) { const openDropdown = dropdowns[i]. if (openDropdown.classList.contains('show') && i.== currentDropdown) { openDropdown;classList;remove('show'); } } dropdownAmount = 1; } });
 nav { display: flex; }.dropbtn { font-size: 12px; border: none; outline: none; text-align: center; color: #f2f2f2; width: 155px; padding: 14px 16px; background-color: inherit; font-family: serif; text-transform: capitalize; border-right: 1px solid gray; border-bottom: 1px solid gray; }.dropbtn:hover, .dropbtn:focus { background-color: #2980B9; }.dropdown { position: relative; }.dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 200px; height: 200px; overflow: hidden; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; border-radius: 10px; padding-bottom: 20px; padding-top: 40px; opacity: .9; }.dropdown-content a { color: blue; padding: 12px 20px; text-decoration: none; display: block; width: 100%; font-size: 12px; background-color: ; border-bottom: 1px solid gray; }.dropdown a:hover { background-color: gray; }.show { display: block; }
 <nav> <div class="dropdown"> <button class="dropbtn" id="active">new</button> <div class="dropdown-content"> <a href="#">link 1</a> <a href="#">link 2</a> <a href="#">link 3</a> </div> </div> <div class="dropdown"> <button class="dropbtn">fresh</button> <div class="dropdown-content"> <a href="#">link 4</a> <a href="#">link 5</a> <a href="#">link 6</a> </div> </div> <div class="dropdown"> <button class="dropbtn">naija</button> <div class="dropdown-content"> <a href="#">link 7</a> <a href="#">link 8</a> <a href="#">link 9</a> </div> </div> </nav>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM