简体   繁体   English

多级下拉菜单切换无法使用jQuery

[英]multilevel dropdown menu toggle not working using jquery

First of all let me tell you I'm little weak in jquery so helping here will let me learn lot about jquery. 首先,我要告诉您,我在jquery方面并不弱,因此在这里提供帮助将使我学到很多有关jquery的知识。 I'm trying to expand the level 2 li menu using jquery but alas after searching google for a while i'd to post my problem in here. 我正在尝试使用jquery扩展2级li菜单,但可惜的是,在搜索google一段时间之后,我想在这里发布我的问题。 so my problem html skeleton is somewhat like below before that i'm working on wp nav. 所以我的问题html骨架有点像下面,在我从事wp导航之前。

<div class="mobile-menu">
    <div id="megaMenu">
        <div id="megaMenuToggle"></div>
        <ul id="megaUber>
            <li>
                <ul>
                    <li>
                        <ul>
                            <li></li>
                            <li></li>
                            <li></li>
                        </ul>
                    </li>
                   <li></li>
                </ul>
             </li>
             <li></li>
             <li></li>
             <li></li>
         </ul>
    </div>
</div>

now the problem is i can open the level 1 li in toggle but while opening the level 2 it's not toggling. 现在的问题是我可以在切换中打开级别1 li,但是在打开级别2时不会切换。 here is my css code for toggling. 这是我的CSS代码进行切换。

.mobile-menu #megaMenu #megaUber > li.active > a + ul {
    display: block !important;}

.mobile-menu #megaMenu #megaUber > li > a + ul {
    display: none !important;}

.mobile-menu #megaMenu #megaUber > li > ul > li.actives > a + ul {
    display: block !important;}
.mobile-menu #megaMenu #megaUber > li > ul > li > a + ul {
    display: none !important}

here is the jquery code that i've written which is working for the level 1 li & megaMenuToggle div toggle but not for level 2. 这是我编写的适用于1级li&megaMenuToggle div切换的jQuery代码,但不适用于2级。

jQuery(function($){
    $(".mobile-menu #megaMenu #megaMenuToggle").on('click', function(){
      $(".mobile-menu #megaMenu #megaUber").slideToggle( "slow" );
    });

    $('.mobile-menu #megaMenu #megaUber li a').on('click',function(event){
      console.log('first drop');
      event.stopPropagation();
      $(".mobile-menu #megaMenu #megaUber li")
        .not($(this).parent())
        .removeClass("active");
        $(this).parent().toggleClass("active");

    });

    $('.mobile-menu #megaMenu #megaUber li ul li a').on('click',function(event){
      console.log('second drop');
      event.stopPropagation();
      $("#megaUber li")
        .not($(this).parent())
        .removeClass("active");
        $(this).parent().toggleClass("active");

      $("#megaUber li ul li")
        .not($(this).parent())
        .removeClass("actives");
        $(this).parent().toggleClass("actives");
    });
});

Ok I've got a working version: Fiddle 好的,我有一个工作版本: Fiddle

The Fix: 解决方法:

$(function () {
    $('#megaUber').find('a').click(function (e) {
        e.stopPropagation();
        $(this).parent().toggleClass("active");
    });
});

And this bit of CSS needed actives changed to active 而这部分CSS所需的actives变为active

.mobile-menu #megaMenu #megaUber > li > ul > li.active > a + ul {
    display: block !important;
}

This should nest infinitely. 这应该无限嵌套。

Here's what's happening: 这是正在发生的事情:

The jQuery looks for the list with the right id . jQuery查找具有正确ID的列表。 I took out the other selector text because an id should only exist once on a page so there's no need to be more specific in the selector, which is what you were doing. 我删除了其他选择器文本,因为id在页面上应该只存在一次,因此您在选择器中不需要更具体。 Once it finds that element it looks into all it's children with the find() function and attaches click() events to all of them. 一旦找到该元素,它将使用find()函数find()所有子元素,并将click()事件附加到所有元素。 Those click events reference their parent() element which goes only one level up, and puts the active class on them. 这些单击事件引用其上一级的parent()元素,并将active类放在其上。 That opens your menu. 打开菜单。

The slideToggle bit just doesn't work like this. slideToggle位只是不能这样工作。 If you want that effect you'll have to rethink how the whole thing works. 如果您想要这种效果,则必须重新考虑整个过程。 I won't go into that. 我不会说的。

I'm not sure what you're trying to do with UberMenu, I've never used it. 我不确定您要使用UberMenu做什么,我从未使用过。 But you must have links in your html, not just <li> elements. 但是您必须在html中包含链接,而不仅仅是<li>元素。

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

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