简体   繁体   English

jQuery下拉菜单显示/隐藏多个

[英]Jquery dropdown menu show/hide multiple

I've been looking through a lot of questions/answers, but still couldn't solve my issue. 我一直在寻找很多问题/答案,但仍然无法解决我的问题。 So I have a menu with JS dropdown (on click, not hover), but if I open a dropdown and I click onto the second menu, then another dropdown will open, but the previous one will not close. 因此,我有一个带有JS下拉菜单的菜单(单击时,没有悬停),但是如果我打开一个下拉菜单,然后单击第二个菜单,则会打开另一个下拉菜单,但上一个不会关闭。

Clicking outside of the menu closes the dropdown, which is great but it's not enough, I want the first dropdown to close even when clicking on the second one! 单击菜单外部将关闭下拉菜单,这很好,但这还不够,我希望即使单击第二个下拉菜单也要关闭第一个下拉菜单!

<li><a onclick="myFunction_tools()" class="dropmenu" href="#">Tools</a></li>
<div id="myToolsDropdown" class="down">
    <div>
        Content here...
    </div>
</div>
<li><a onclick="myFunction_forum()" class="dropmenu" href="#">Forum</a></li>
<div id="myForumDropdown" class="down">
    <div>
        Content here...
    </div>
</div>

Here is the JS: 这是JS:

function myFunction_tools() {
    document.getElementById("myToolsDropdown").classList.toggle("show");
}

function myFunction_forum() {
    document.getElementById("myForumDropdown").classList.toggle("show");
}

window.onclick = function(event) {
  if (!event.target.matches('.dropmenu')) {

    var dropdowns = document.getElementsByClassName("down");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

$(".down").click(function(e){
  e.stopPropagation();
});

EDIT 2: 编辑2:

Here is a solution, that should work for you: 这是一个解决方案,应该适合您:

 $(document).ready(function() { $('.dropmenu').click(function(e) { e.preventDefault(); $('.down').hide(); $(e.target).next('.down').show(); }); }); 
 .down { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <li><a class="dropmenu" href="#">Tools</a> <div id="myToolsDropdown" class="down"> <div> Content here... </div> </div></li> <li><a class="dropmenu" href="#">Forum</a> <div id="myForumDropdown" class="down"> <div> Content here... </div> </div></li> 

Original: 原版的:

You prevent closing of all dropdowns, when clicking on a dropdown: 单击下拉列表时,可以防止关闭所有下拉列表:

if (!event.target.matches('.dropmenu')) 如果(!event.target.matches('。dropmenu'))

You have to change the condition, so it matches only the current active dropdown. 您必须更改条件,因此它仅与当前活动下拉列表匹配。 Like " if the event target matches the current active dropdown, prevent action ". 类似于“ if the event target matches the current active dropdown, prevent action ”。 You could determine the current active dropdown, by checking for the open class. 您可以通过检查open类来确定当前的活动下拉列表。

EDIT: You might only need to change the condition to: 编辑:您可能只需要将条件更改为:

if (!event.target.matches('.dropmenu.active'))

so it closes all dropdowns on clicks, that are not on .dropmenu.active and than triggers the click on down. 因此,它会关闭所有未位于.dropmenu.active点击的下拉菜单,然后触发点击。

Also, because you seem to be using jQuery anyway, you could optimize your code a lot by using jQuery throughout your script: 另外,由于您似乎仍在使用jQuery,因此可以在整个脚本中使用jQuery来优化代码:

$(window).click(fucntion(event) {
    var $target = $(event.target);
    if(!$target.hasClass('show')){
        $('.down').removeClass('show');
        $target.addClass('show');
    }
});

Just remove the show class of the previous dropdown on your functions. 只需删除函数上一个下拉列表的show类。 For example: 例如:

function myFunction_tools() {
    document.getElementById("myToolsDropdown").classList.toggle("show");
    document.getElementById("myForumDropdown").classList.remove("show");
}

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

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