简体   繁体   English

在jQuery中切换多个下拉菜单

[英]Toggle Multiple Drop Down Menus in jQuery

I've got a set of menus that I would like to toggle on/off when a user clicks on an option bar (similar to the bar in Gmail which you move/label emails). 我有一组菜单,当用户单击选项栏(类似于您在Gmail中移动/标记电子邮件的栏)时,我想打开/关闭这些菜单。

When a user clicks a button, the menu appears. 用户单击按钮时,将显示菜单。 When the user clicks another button, the previous menu disappears and the new one appears. 当用户单击另一个按钮时,上一个菜单消失,出现一个新菜单。 This is working with the following code. 这正在使用以下代码。 However, what isn't working is if the user clicks a button, then clicks the same button, the menu is not disappearing. 但是,不起作用的是,如果用户单击一个按钮,然后单击相同的按钮,则菜单不会消失。

I understand why it's not working, but not sure how to fix it. 我了解为什么它不起作用,但是不确定如何修复它。 I think I need to add "if the open menu is the one you are clicking on, close it and dont open anything else." 我想我需要添加“如果打开的菜单是您要单击的菜单,请关闭它,不要打开其他任何菜单。” But the if statements i've tried aren't doing this. 但是我尝试过的if语句并没有这样做。

$("#moving ul li").click(function() {
    // If the button clicked has the "active" class (meaning the menu is already open)
    if ($("#moving ul li.active") == this) {
        // Close the menu (removing the visible class) and make the button no longer active
        $(this).toggleClass("active")
        $(this).children("ul").toggleClass("visible")
    // Otherwise ...
    } else {
        // Find the open one and close it
        $("#moving ul li.active").toggleClass("active")
        $("#moving ul li ul.visible").toggleClass("visible")
        // Open this menu
        $(this).toggleClass("active")
        $(this).children("ul").toggleClass("visible")
    }
})

It's not working because your if statement will never match. 它不起作用,因为您的if语句将永远不匹配。

"this" isn't a jQuery object, it's the actual DOM node. “ this”不是jQuery对象,而是实际的DOM节点。 The statement should be: 该声明应为:

if($(this).hasClass('active')){

try this 尝试这个

html html

<ul class="nav">
    <li>
       <a href="#">Main 1</a>
       <a class='trigger'><div></div></a>
       <ul>
          <li>
             <a href="#">sub1</a>
             <a class='trigger'><div></div></a>
              <ul>
                  <li>
                         <a href="#">subsub1</a>
                  </li>
               </ul>
           </li>
       </ul>
    </li>

</ul>

css code CSS代码

<style>
.nav li{list-style:none;display:block}
.nav > li ul{display:none}
.nav a.trigger{display:block;float:right;margin-                      right:2px;width:20px;height:20px}
.nav a.minus > div{background:url(minus.png);}
.nav a.plus > div{background:url(plus.png);}
</style>

Jquery Code jQuery代码

$(".nav a.trigger").addClass("plus");
$(".nav a.trigger").click(function(){ 
    var ul = $(this).parent().children("ul");
    if($(this).siblings('ul').is(":hidden")){
        $(this).siblings('ul').slideDown();
        $(this).addClass("minus");
        $(this).removeClass("plus");
    }else{
        $(this).siblings('ul').slideUp();
        $(this).addClass("plus");
        $(this).removeClass("minus");
    }
         $(this).parent().siblings('li').find("ul:visible").slideUp().siblings('a.trigger').addClass("plus");
      $(this).parent().siblings('li').find("ul:visible").slideUp().siblings('a.trigger').addClass("minus");
    $(this).siblings('ul').find("ul:visible").slideUp().siblings('a.trigger').addClass("plus");
    $(this).siblings('ul').find("ul:visible").slideUp().siblings('a.trigger').removeClass("minus");
});

Hey Brenden, I got a nice solution for you :) Your problem is little bit tricky... 嘿,布伦登,我为您提供了一个不错的解决方案:)您的问题有点棘手...

// JS - jQuery
$(function () {

    $("#moving .list h1").click(function() { // only recognize the click on headlines in lists

        $(this).next().toggle(); // get the ul and toggle

        if($('.list ul:visible').length > 1) { // reset all if there is more than one opened
            $('.list ul:visible').hide(); // hide all
            $(this).next().show(); // now show only the list you want
        }
    }); 
});

// HTML
<div id="moving">
    <div class="list">
        <h1>Hi im first</h1>
        <ul>
            <li>Foo1</li>
            <li>Bar1</li>
        </ul>
    </div>
    <div class="list">
        <h1>Hi im second</h1>
        <ul>
            <li>Foo2</li>
            <li>Bar2</li>
        </ul>
    </div>
</div>

This would be my solution for your problem. 这将是我为您解决的问题。 Its a little bit different, because its not a good practise to use Unordered Lists ( <ul> ) in a List Item ( <li> ). 有点不同,因为在列表项( <li> )中使用无序列表( <ul> )不是一个好习惯。 So I gave every element an Headline ( <h1> ) and a container ( <div id="list"> ). 所以我给每个元素一个标题( <h1> )和一个容器( <div id="list"> )。

Hope that could help you to solve problems in future. 希望可以帮助您将来解决问题。

If you have any question ask in comment :) 如果您有任何疑问,请在评论中提问:)

http://jsfiddle.net/jjyss/ <- Thats an Live Demo for you http://jsfiddle.net/jjyss/ <-就是您的现场演示

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

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