简体   繁体   English

如何使这个 onClick function 用于多个下拉菜单?

[英]How do I make this onClick function work for multiple dropdown menus?

I've never tried Javascript before and looked around, but the tutorials I've found would take me weeks to figure out (attention/focus issues + I don't even know what words I want to search for) and none of the solutions I've searched for solved it, and I don't know enough to extrapolate it from other answers.我以前从未尝试过 Javascript 并环顾四周,但我发现的教程需要我数周的时间才能弄清楚(注意/焦点问题 + 我什至不知道我想搜索什么词)并且没有解决方案我已经搜索解决了它,但我不知道如何从其他答案中推断出来。

Can someone give me an example of this code (from w3School) extended to also toggle more dropdown menus?有人可以给我一个扩展此代码(来自 w3School)以切换更多下拉菜单的示例吗? It has to be usable with keyboard like this one is.它必须像这个一样可以与键盘一起使用。

Currently it's only handling the menu with an ID of "dropperso" and can open the Personal menu, I need the "openMenu" function to also react to the ID "dropsites" and be able to open the Other Sites menu.目前它只处理 ID 为“dropperso”的菜单并且可以打开个人菜单,我需要“openMenu”function 来响应 ID“dropsites”并能够打开其他站点菜单。 A note that the button and affected ID-having div are siblings.请注意,按钮和受影响的具有 ID 的 div 是兄弟姐妹。

No JQuery please.没有 JQuery 请。

JS:记者:

function openMenu() {
  document.getElementById("dropperso").classList.toggle("dropopen");
}

HTML: HTML:

          <div class="dropdown">
            <button onclick="openMenu()" class="drophover">Other Sites</button>
            <div id="dropsites" class="dropdown-content">
              A link
            </div>
          </div>
          <div class="dropdown">
            <button onclick="openMenu()" class="drophover">Personal</button>
            <div id="dropperso" class="dropdown-content" style="right: 0;">
              A link
              A link
            </div>
          </div>

All that the.dropopen css class does is change the display of.dropdown-content from none to block. the.dropopen css class 所做的只是将 .dropdown-content 的显示从无更改为阻止。

I tried to search for my specific problem and all I found was either way beyond my ability to understand, "use JQuery" (I'm limited and can't use JQuery), or "use this other code (that doesn't work for mine)".我试图搜索我的具体问题,但我发现的都是超出我理解能力的方式,“使用 JQuery”(我受限并且不能使用 JQuery),或者“使用其他代码(不起作用)对于我的)”。

It works if I just copy the whole thing and make one function for each menu, but I get the feeling that's kinda bad spaghetti coding, and I can't compress this on my own without an example that works to learn from.如果我只是复制整个东西并为每个菜单制作一个 function,它会起作用,但我觉得这有点糟糕的意大利面条编码,如果没有一个可以学习的例子,我不能自己压缩它。

I'd be VERY grateful if you could solve that for me so I can use that later, and even MORE grateful if you could either explain how you made it work or link to the specific parts of documentation that explain what you're using.如果您能为我解决这个问题,我将非常感激,以便我以后可以使用它,如果您能解释您如何使其工作或链接到解释您正在使用的内容的文档的特定部分,我将更加感激。

function openMenu(id) {
  document.getElementById(id).classList.toggle("dropopen");
}
<button onclick="openMenu('dropsites')" class="drophover">Personal</button>
<button onclick="openMenu('dropperso')" class="drophover">Personal</button>

If you want to toggle them all with one click use querySelectors to get all the dropdown menus and toggle each of them like this:如果您想一键切换它们,请使用 querySelectors 获取所有下拉菜单并像这样切换它们中的每一个:

const dropdownMenus = document.querySelectorAll(".dropdown-content")

for(const menu of dropdownMenus){
   menu.classList.toggle("dropopen")
}

but if you want to toggle each of them with same function and not writing a function for each menu you can do like this:但是如果你想用相同的 function 切换它们中的每一个而不是为每个菜单写一个 function 你可以这样做:

JS :记者

function openMenu(id) {
  document.getElementById(id).classList.toggle("dropopen");
}

HTML : HTML :

  <div class="dropdown">
    <button onclick="openMenu('dropsites')" class="drophover">Other Sites</button>
    <div id="dropsites" class="dropdown-content">
      A link
    </div>
  </div>
  <div class="dropdown">
    <button onclick="openMenu('dropperso')" class="drophover">Personal</button>
    <div id="dropperso" class="dropdown-content" style="right: 0;">
      A link
      A link
    </div>
  </div>
     <div class="drop-down flex-row-AI-center" data-dropdown>
    <button class="drop-btn" data-dropdownBtn>Categories</button>
    <div class="dropdown-content flex-col" data-dropdown-content>
      <a href="#action">Action </a>
      <a href="#adventure">Adventure</a>
      <a href="#anime">Anime</a>
      <a href="#comedy">Comedy</a>
      <a href="#thriller">Thriller</a>
      <a href="#fantasy">Fantasy</a>
    </div>
  </div>
</div>

You have to give all the dropdown same ids/class/dataset-attributes您必须为所有下拉列表提供相同的 ids/class/dataset-attributes

 function toggleDropDown(e) {
const isDropdownBtn = e.target.matches('[data-dropdownBtn]');

//as long as user clicking inside of dropdown it won't close
if (!isDropdownBtn && e.target.closest('[data-dropdown]') != null) return;

let currDropdown;

if (isDropdownBtn) {
currDropdown = e.target.closest('[data-dropdown]');
currDropdown.classList.add('active');
}

document.querySelectorAll('[data-dropdown-content].active').forEach(dropdowm => {
if(currDropdown === dropdowm) return
dropdowm.classList.remove('active')
  })
}

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

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