简体   繁体   English

无法使用 querySelectorAll 打开下拉菜单

[英]Can't open dropdown menu using querySelectorAll

I have a mobile navigation with two dropdown menus.我有一个带有两个下拉菜单的移动导航。 Here is the markup of the nav:这是导航的标记:

<div id="mobile-menu" class="mobile-menu container fixed">
                    <ul>
                        <li><a href="#">Home</a></li>
                        <li class="dropdown">
                            <a href="#">Articles <i class="bi bi-chevron-down"></i></a>
                            <ul class="submenu hidden">
                                <li><a href="#">Submenu item 1</a></li>
                                <li><a href="#">Submenu item 2</a></li>
                            </ul>
                        </li>
                        <li><a href="#">Contact</a></li>

                        <li class="dropdown">
                            <a href="#">My account <i class="bi bi-chevron-down"></i></a>
                            <ul class="submenu hidden">
                                <li><a href="#">Dashboard</a></li>
                                <li><a href="#">Profile</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>

The dropdown menus should open/expand when clicked.单击下拉菜单应打开/展开。 Originally I grabbed the dropdown and submnenu classes like this:最初我抓住了这样的下拉和子菜单类:

const mobileDropdown = document.querySelector(".dropdown");
const mobileSubMenu = document.querySelector('.submenu');

and used an eventlistener to toggle the "hidden" class which is just a display:none并使用事件监听器来切换只是一个显示的“隐藏”类:无

mobileDropdown.addEventListener('click', () => {
    mobileSubMenu.classList.toggle('hidden');
});

The problem with this is that this will only open the first dropdown menu and I cannot open the second.问题在于这只会打开第一个下拉菜单,而我无法打开第二个。

When I try to use querySelectorAll instead of just querySelector then i get thiserror:当我尝试使用 querySelectorAll 而不是仅使用 querySelector 时,我收到此错误:

Uncaught typeError addEventListener is not a function未捕获的 typeError addEventListener 不是函数

Here I read that with querySelectorAll I need to use a for or foreach loop. 在这里,我使用 querySelectorAll 阅读了我需要使用 for 或 foreach 循环。

but i think I'm messing it up.但我想我搞砸了。 I tried this:我试过这个:

const mobileDropdown = document.querySelectorAll(".dropdown");
const mobileSubMenu = document.querySelector('.submenu');

mobileDropdown.forEach(md => md.addEventListener('click', () => {
    mobileSubMenu.classList.toggle('hidden');
}));

Now I don't get an error, I can open the first dropdown menu, but when I try to open the second dropdown menu, the first one opens.现在我没有收到错误,我可以打开第一个下拉菜单,但是当我尝试打开第二个下拉菜单时,第一个会打开。 What am I doing wrong?我究竟做错了什么? Thanks in advance.提前致谢。

The problem in your code is that each mobileDropdown 's clickEvent was linked to the first submenu , you should link mobileDropdown 's clickEvents to their submenu children like that您的代码中的问题是每个mobileDropdown的 clickEvent 都链接到第一个子submenu ,您应该像这样将mobileDropdown的 clickEvents 链接到他们的submenu

 document.querySelectorAll(".dropdown").forEach(md => md.addEventListener('click', () => { md.querySelector(".submenu").classList.toggle('hidden'); }));
 .hidden { visibility: hidden; }
 <div class="mobile-menu container fixed" id="mobile-menu"> <ul> <li><a href="#">Home</a></li> <li class="dropdown"> <a href="#">Articles <i class="bi bi-chevron-down"></i></a> <ul class="submenu hidden"> <li><a href="#">Submenu item 1</a></li> <li><a href="#">Submenu item 2</a></li> </ul> </li> <li><a href="#">Contact</a></li> <li class="dropdown"> <a href="#">My account <i class="bi bi-chevron-down"></i></a> <ul class="submenu hidden"> <li><a href="#">Dashboard</a></li> <li><a href="#">Profile</a></li> </ul> </li> </ul> </div>

here is a solution.这是一个解决方案。 You have to listen to the 'click' event for "submenu" on the element of "dropdown".您必须在“下拉”元素上收听“子菜单”的“点击”事件。

const mobileDropdown = document.querySelectorAll(".dropdown");

  mobileDropdown.forEach((md) => {
    md.addEventListener("click", () => {
      const mobileSubMenu = md.querySelector(".submenu");
      mobileSubMenu.classList.toggle("hidden");
    });
  });

mobileSubMenu 永远是第一位的!

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

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