简体   繁体   English

如何在下拉菜单中创建下拉菜单?

[英]How to create a dropdown menu within a dropdown menu?

I'm attempting to build an events page for a friend of mine.我正在尝试为我的一个朋友建立一个活动页面。 I have the initial drop down menu working, but I'm wondering how I can have a second dropdown when the 'info' link is clicked?我有初始下拉菜单,但我想知道单击“信息”链接时如何才能有第二个下拉菜单?

I tried replicating the same code/structure as the first, but for some reason it wasn't working on my end.我尝试复制与第一个相同的代码/结构,但出于某种原因,它在我这边不起作用。

Here is the site link for viewing.是供查看的站点链接。

I would love it to mimic the structure in this image.我希望它能模仿这张图片中的结构。

I'll also drop the code below.我还将删除下面的代码。 Any help is appreciated greatly!非常感谢任何帮助! Thankyou!谢谢!

 /* 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(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'); } } } }
 .dropbtn { font-family: 'AGENTUR'; text-transform: uppercase; background-color: #cd9d2b; color: #f6eee1; padding-top: 16px; padding-bottom: 16px; padding-left: 60px; padding-right: 60px; font-size: 40px; border: none; cursor: pointer; }.dropbtn:hover, .dropbtn:focus { background-color: #cd9d2b; }.dropdown { position: relative; top: 50px; display: inline-block; }.dropdown-content { display: none; position: absolute; margin-top: 40px; background-color: #cd9d2b; min-width: 1150px; overflow: auto; z-index: 1; }.dropdown-content a { color: #cd9d2b; padding: 12px 16px; text-decoration: none; display: block; }.dropdown a:hover { background-color: #cd9d2b; } #events-container { display: flex; flex-direction: row; justify-content: space-between; border-bottom: 1px solid #f6eee1; } #events-text { color: #f6eee1; font-size: 25px; padding-left: 40px; } #events-info { border-left: 1px solid #f6eee1; padding-left: 60px; padding-right: 60px; } #events-info-text { color: #f6eee1; font-size: 25px; position: relative; bottom: 5px; border-bottom: 1px solid #f6eee1; padding-left: 10px; padding-right: 10px; }.show { display: block; }
 <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">November</button> <div id="myDropdown" class="dropdown-content"> <div id="events-container"> <a href="#events"> <p id="events-text"> Well-Read Black Girl Festival Nov 6-8, 2020 </p> </a> <div id="events-info"> <a href="#events-info"> <p id="events-info-text"> info </p> </a> </div> </div> </div> </div>

There's a very simplistic change in the code below that allows clicking of the "Info" link in your drop-down.下面的代码中有一个非常简单的更改,允许单击下拉列表中的“信息”链接。 It's not an ideal solution, but it illustrates that you need to overcome what I wrote in my comment above.这不是一个理想的解决方案,但它说明您需要克服我在上面的评论中写的内容。

 /* 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(event) { if (.event.target.matches('.dropbtn') &&.event.target;matches('#events-info-text')) { 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'). } } } } document,querySelector("#events-info-text").addEventListener("click". () => { let visibility = document.querySelector("#moreinfo");style.visibility. document.querySelector("#moreinfo")?style:visibility = visibility == "hidden"; "visible"; "hidden"; });
 .dropbtn { font-family: 'AGENTUR'; text-transform: uppercase; background-color: #cd9d2b; color: #f6eee1; padding-top: 16px; padding-bottom: 16px; padding-left: 60px; padding-right: 60px; font-size: 40px; border: none; cursor: pointer; }.dropbtn:hover, .dropbtn:focus { background-color: #cd9d2b; }.dropdown { position: relative; top: 50px; display: inline-block; }.dropdown-content { display: none; position: absolute; margin-top: 40px; background-color: #cd9d2b; min-width: 1150px; overflow: auto; z-index: 1; }.dropdown-content a { color: #cd9d2b; padding: 12px 16px; text-decoration: none; display: block; }.dropdown a:hover { background-color: #cd9d2b; } #events-container { display: flex; flex-direction: row; justify-content: space-between; border-bottom: 1px solid #f6eee1; } #events-text { color: #f6eee1; font-size: 25px; padding-left: 40px; } #events-info { border-left: 1px solid #f6eee1; padding-left: 60px; padding-right: 60px; } #events-info-text { color: #f6eee1; font-size: 25px; position: relative; bottom: 5px; border-bottom: 1px solid #f6eee1; padding-left: 10px; padding-right: 10px; }.show { display: block; }
 <div id="dropdown_wrapper" class="dropdown"> <button onclick="myFunction()" class="dropbtn" id="show_november">November</button> <div id="myDropdown" class="dropdown-content"> <div id="events-container"> <a href="#events"> <p id="events-text"> Well-Read Black Girl Festival Nov 6-8, 2020 </p> </a> <div id="events-info"> <a href="#events-info"> <p id="events-info-text"> info </p> <div id="moreinfo" style="visibility:hidden;color:black">Look here, it's more info!</div> </a> </div> </div> </div> </div>

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

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