简体   繁体   English

如何切换此下拉菜单?

[英]How can I toggle this dropdown?

I have tried adding a simple toggle function to the dropdown-btn class which in turn adds the active class (which is set to display: block;) onto the ul class, am I doing anything wrong here? I have tried adding a simple toggle function to the dropdown-btn class which in turn adds the active class (which is set to display: block;) onto the ul class, am I doing anything wrong here?

 const dropdown = document.querySelector('.dropdown-btn'); dropdown.addEventListener('click', function() { dropdown.classList.toggle('active'); });
 .sidebar-nav { display: grid; gap: 15px; padding: 15px; }.sidebar-nav li { background: #f0f7ff; border-radius: 4px; line-height: 2; cursor: pointer; font-weight: 500; transition: 150ms ease; position: relative; }.options { display: none; }.options.active { display: block; }
 <nav class="admin-sidebar sidebar"> <div class="sidebar-nav"> <li><a href="#" class="dropdown-btn"><i class="fas fa-address-card"></i><span>Dropdown</span> <i class="fas fa-angle-down"></i></a></li> <ul class="options"> <li>Option 1</li> <li>Option 2</li> <li>Option 3</li> </ul> </div> <nav/>

When you click on the button you have to toggle the active class on the ul element, wich you have set display:none .当您单击按钮时,您必须在ul元素上切换活动的 class ,您已设置display:none

 const dropdown = document.querySelector('.dropdown-btn'); const options = document.querySelector('.options'); dropdown.addEventListener('click', function() { options.classList.toggle('active'); });
 .sidebar-nav { display: grid; gap: 15px; padding: 15px; }.sidebar-nav li { background: #f0f7ff; border-radius: 4px; line-height: 2; cursor: pointer; font-weight: 500; transition: 150ms ease; position: relative; }.options { display: none; }.options.active { display: block; }
 <nav class="admin-sidebar sidebar"> <div class="sidebar-nav"> <li><a href="#" class="dropdown-btn"><i class="fas fa-address-card"></i><span>Dropdown</span><i class="fas fa-angle-down"></i></a></li> <ul class="options"> <li>Option 1</li> <li>Option 2</li> <li>Option 3</li> </ul> </div> <nav/>

Try adding this in your function and then change the event to focus and then create another function by blur and change display to none bro:尝试将其添加到您的 function 中,然后将事件更改为焦点,然后通过模糊创建另一个 function 并将显示更改为无兄弟:

document.getElementsByClassName('optioons').style.display = 'block';

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

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