简体   繁体   English

单击两次关闭菜单下拉菜单

[英]Close menu dropdown by clicking on it twice



I have a vertical menu with some dropdown.我有一个带有一些下拉菜单的垂直菜单。 Actually more dropdowns can't be opened simultaneously, and, if a dropdown is open, when I click on another dropdown-button, the dropdown closes himself.实际上,无法同时打开更多下拉菜单,并且,如果打开了下拉菜单,当我单击另一个下拉按钮时,下拉菜单会自行关闭。
You can see a demo here .您可以在此处查看演示

The problem is that now, if I click twice on the same dropdown button, the first time I click the dropdown open itself (as it should be), but the second time the dropdown doesn't close itself as I want.问题是,现在,如果我在同一个下拉按钮上单击两次,第一次单击下拉菜单时会自动打开(应该如此),第二次下拉菜单不会按照我的意愿自行关闭
Furthermore, also when the dropdown is open and I click on another button on the menu the dropdown doesn't close itself.此外,当下拉菜单打开并且我单击菜单上的另一个按钮时,下拉菜单不会自行关闭。
Here you can see my code:在这里你可以看到我的代码:

<div class="sidenav">
  <a href="#about">About</a>
  <a href="#services">Services</a>
  <a href="#clients">Clients</a>
  <a href="#contact">Contact</a>
  <button class="dropdown-btn">Dropdown 
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-container">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <button class="dropdown-btn">Dropdown 
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-container">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <a href="#contact">Search</a>
</div>

var accItem = document.getElementsByClassName('dropdown-container');
    var accHD = document.getElementsByClassName('dropdown-btn');
    for (i = 0; i < accHD.length; i++) {
        accHD[i].addEventListener('click', toggleItem, false);
    }
    function toggleItem() {

        var itemClass = this.nextElementSibling;
        for (i = 0; i < accItem.length; i++) {
                accItem[i].style.display = "none";
            accHD[i].classList.remove("active");
        }
        if (itemClass.style.display === "none") {
                this.classList.add("active");
            //this.classList.toggle("active");
            this.nextElementSibling.style.display = "block";
        }

    }

Is someone able to find where the problem is and solve it?有人能够找到问题所在并解决它吗?
Thank you, regards谢谢,问候

(Hope it is clear, if not, let me know in the comments. I do not speak English very well.) (希望很清楚,如果没有,请在评论中告诉我。我的英语不太好。)

Above your for loop try adding:在您的 for 循环上方尝试添加:

if (itemClass.style.display === "block") {
    this.nextElementSibling.style.display = "none";
    this.classList.remove("active");
    return;
}

This will check to see if the item clicked is already open and handle setting all your values.这将检查单击的项目是否已经打开并处理设置所有值。 By adding the return it will prevent the rest of the code from being executed.通过添加返回,它将阻止执行其余代码。

Here is a fiddle with your code and answering your other question about closing them on click of one of the main links:这是对您的代码的摆弄,并回答了有关单击主要链接之一关闭它们的其他问题:

fiddle小提琴

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

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