简体   繁体   English

打开另一个下拉菜单时如何关闭下拉菜单

[英]How to make dropdown menu close when opening another one

Hy.Im a iniciant in javascript. Hy.Im 是 javascript 的创始人。 I'm having trouble closing the dropdown when clicking on another open one.单击另一个打开的下拉菜单时,我无法关闭下拉菜单。

I try this:我试试这个:

        on('click', '.navbar .dropdown > a', function(e) {
        if (select('#navbar').classList.contains('navbar-mobile')) {
          e.preventDefault()
          this.nextElementSibling.classList.toggle('dropdown-active')
        } else {
    this.previousElementSibling.classList.remove('dropdown-active');
    } 
      }, true)

dropdown image opened items Codepen code下拉图片打开的项目Codepen代码

Thank you for any help!感谢您的任何帮助!

a few days later.几天后。 this is my solution:这是我的解决方案:

Use querySelectorAll + forEach in .dropdown-active and then remove the class.dropdown-active中使用querySelectorAll + forEach然后删除类

on('click', '.navbar .dropdown > a', function (e) {
    if (select('#navbar').classList.contains('navbar-mobile')) {
      e.preventDefault()
      document.querySelectorAll(".dropdown-active").forEach(function (element) {
        element.classList.remove("dropdown-active");
      });
      this.nextElementSibling.classList.toggle('dropdown-active')
    }
  }, true)

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

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