如何通过点击显示菜单孙子?

时间:2018-04-06 15:24:08

标签: javascript html css

我相信这个问题与JavaScript / Jquery 3.3.1有关,而不是评估菜单的孙子,但我对如何完成这项工作感到很遗憾。任何帮助,将不胜感激。

HTML是在Drupal 7中创建的菜单块。我的CSS中没有使用叶子类。

<div id="block-menu-menu-demo" class="block block-menu contextual-links-region">
<h2>Family &amp; Community Menu</h2>
<ul class="menu">
    <li class="first leaf"><a href="/fce">Main Page</a></li>
    <li class="leaf"><a href="/sde/families">For Families</a></li>
    <li class="leaf"><a href="/sde/schools">For Schools</a></li>
    <li class="leaf"><a href="/sde/communities">For Communities</a></li>
    <li class="last expanded"><a href="/sde/parent-community-engagement">Related Topics</a>
        <ul class="menu">
            <li class="first leaf"><a href="/sde/21cclc">21st CCLC</a></li>
            <li class="leaf"><a href="/sde/summer-learning">Summer Learning</a></li>
            <li class="last expanded"><a href="/sde/chronic-absenteeism">Chronic Absenteeism</a>
                <ul class="menu">
                    <li class="first last leaf"><a href="/absenteeism-tool-kit">Toolkit</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

可以清理CSS,但它可以正常工作。

    /*SIDE NAV*/

#block-menu-menu-demo {
    background-color: #dcdcdc;
    border-radius: 5px;
    color: #FFF;
    display: block;
    font-family: 'RobotoLight', arial, helvetica, sans-serif;
    line-height: .8em;
    margin: 20px auto;
    padding: 0px;
    overflow: hidden;
    width: 250px;
    z-index: 77;
}
#block-menu-menu-demo a {
    border-bottom: none !important;
    color: #FFF !important;
    display: block !important;
    font-size: 14px;
    text-decoration: none;
    padding-left: 5px;
}
#block-menu-menu-demo ul {
    border-radius: 0;
    margin: 0;
}
#block-menu-menu-demo li {
    background-color: #4176c3;
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    list-style: none;
    padding-bottom: 12px;
    padding-left: 0px;
    padding-top: 12px;
}
#block-menu-menu-demo li:hover {
    background-color: #96b9ed;
    color: #235196 !important;
}
/*Adding this creates a hover on li since a is inside the li and we can't change that.*/

#block-menu-menu-demo li:hover>a {
    background-color: transparent !important;
    color: #235196 !important;
}
#block-menu-menu-demo li a:hover {
    color: #235196 !important;
}
#block-menu-menu-demo li a:active {
    color: #000 !important;
}
#block-menu-menu-demo h2 {
    background-color: #2b3c4f;
    border-bottom: none !important;
    color: #FFF;
    font-size: 1em;
    margin: 0;
    padding: 8px 0 8px 12px;
}
/*Expanded subnav action*/

#block-menu-menu-demo h2, #block-menu-menu-demo ul.expanded {
    position: relative;
}
#block-menu-menu-demo h2:after, #block-menu-menu-demo .expanded:after {
    border: solid transparent;
    border-radius: 0;
    border-width: 7px;
    content: " ";
    height: 0;
    left: 10px;
    position: absolute;
    pointer-events: none;
    top: 100%;
    width: 0;
}
#block-menu-menu-demo h2:after {
    border-top-color: #2b3c4f;
}
#block-menu-menu-demo .menu {
    padding-left: 0px;
}
#block-menu-menu-demo .expanded {
    margin-bottom: 0;
}
#block-menu-menu-demo .expanded ul {
    list-style: none;
    margin-top: 12px;
    border-radius: 0;
}
#block-menu-menu-demo ul.expanded:after {
    border-top-color: #69afff;
}
#block-menu-menu-demo .expanded ul.menu {
    display: none;
    width: 250px;
}
#block-menu-menu-demo li.expanded {
    background-image: url('http://www.ok.gov/sde/sites/ok.gov.sde/files/side_menu_droparrow-clear.png');
    background-position: right center;
    background-repeat: no-repeat;/* Or size of icon + spacing */
}
#block-menu-menu-demo li:last-child, #block-menu-menu-demo ul.expanded {
    border-bottom: none;
}
/*Expanded subnav*/

#block-menu-menu-demo li li {
    background-color: #eee;
    padding-left: 5px;/*width: 250px;*/
}
#block-menu-menu-demo li li a {
    color: #555 !important;
}
#block-menu-menu-demo li li a:active {
    color: #000 !important;
}
#block-menu-menu-demo li li:hover {
    background-color: #CCC !important;
}

的Javascript

    // JavaScript Document
$(document).ready(function () {
    $('#block-menu-menu-demo > ul > li > a').click

        (function () {
        $('#block-menu-menu-demo li').removeClass('first');
        $(this).closest('li').addClass('first');
        var checkElement = $(this).next();

        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            $(this).closest('li').removeClass('first');
            checkElement.slideUp('normal');
        }
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $('#block-menu-menu-demo ul ul:visible').slideUp

                ('normal');
            checkElement.slideDown('normal');
        }
        if ($(this).closest('li').find('ul').children().length === 0) {
            return true;
        } else {
            return false;
        }
    });
});

1 个答案:

答案 0 :(得分:0)

您的问题出在您的选择器中。你太具体了。点击#block-menu-menu-demo > ul > li > a的选择器仅适用于菜单结构第一级的锚点。更深层次的嵌套锚点甚至不会触发它。

尝试“关闭”打开的菜单时,您也有些奇怪。同样,这个选择器#block-menu-menu-demo ul ul:visible太具体了。我重写了那部分以使用siblings()

$(document).ready(function () {
        $('#block-menu-menu-demo a').click

            (function () {
            $('#block-menu-menu-demo li').removeClass('first');
            $(this).closest('li').addClass('first');
            var checkElement = $(this).next();

            if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                $(this).closest('li').removeClass('first');
                checkElement.slideUp('normal');
            }
            if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                $(this).siblings('ul:visible').slideUp('normal');
                checkElement.slideDown('normal');
            }
            if ($(this).closest('li').find('ul').children().length === 0) {
                return true;
            } else {
                return false;
            }
        });
    });
/*SIDE NAV*/

    #block-menu-menu-demo {
        background-color: #dcdcdc;
        border-radius: 5px;
        color: #FFF;
        display: block;
        font-family: 'RobotoLight', arial, helvetica, sans-serif;
        line-height: .8em;
        margin: 20px auto;
        padding: 0px;
        overflow: hidden;
        width: 250px;
        z-index: 77;
    }
    #block-menu-menu-demo a {
        border-bottom: none !important;
        color: #FFF !important;
        display: block !important;
        font-size: 14px;
        text-decoration: none;
        padding-left: 5px;
    }
    #block-menu-menu-demo ul {
        border-radius: 0;
        margin: 0;
    }
    #block-menu-menu-demo li {
        background-color: #4176c3;
        border-bottom: 1px solid rgba(0, 0, 0, 0.2);
        list-style: none;
        padding-bottom: 12px;
        padding-left: 0px;
        padding-top: 12px;
    }
    #block-menu-menu-demo li:hover {
        background-color: #96b9ed;
        color: #235196 !important;
    }
    /*Adding this creates a hover on li since a is inside the li and we can't change that.*/

    #block-menu-menu-demo li:hover>a {
        background-color: transparent !important;
        color: #235196 !important;
    }
    #block-menu-menu-demo li a:hover {
        color: #235196 !important;
    }
    #block-menu-menu-demo li a:active {
        color: #000 !important;
    }
    #block-menu-menu-demo h2 {
        background-color: #2b3c4f;
        border-bottom: none !important;
        color: #FFF;
        font-size: 1em;
        margin: 0;
        padding: 8px 0 8px 12px;
    }
    /*Expanded subnav action*/

    #block-menu-menu-demo h2, #block-menu-menu-demo ul.expanded {
        position: relative;
    }
    #block-menu-menu-demo h2:after, #block-menu-menu-demo .expanded:after {
        border: solid transparent;
        border-radius: 0;
        border-width: 7px;
        content: " ";
        height: 0;
        left: 10px;
        position: absolute;
        pointer-events: none;
        top: 100%;
        width: 0;
    }
    #block-menu-menu-demo h2:after {
        border-top-color: #2b3c4f;
    }
    #block-menu-menu-demo .menu {
        padding-left: 0px;
    }
    #block-menu-menu-demo .expanded {
        margin-bottom: 0;
    }
    #block-menu-menu-demo .expanded ul {
        list-style: none;
        margin-top: 12px;
        border-radius: 0;
    }
    #block-menu-menu-demo ul.expanded:after {
        border-top-color: #69afff;
    }
    #block-menu-menu-demo .expanded ul.menu {
        display: none;
        width: 250px;
    }
    #block-menu-menu-demo li.expanded {
        background-image: url('files/side_menu_droparrow-clear.png');
        background-position: right center;
        background-repeat: no-repeat;/* Or size of icon + spacing */
    }
    #block-menu-menu-demo li:last-child, #block-menu-menu-demo ul.expanded {
        border-bottom: none;
    }
    /*Expanded subnav*/

    #block-menu-menu-demo li li {
        background-color: #eee;
        padding-left: 5px;/*width: 250px;*/
    }
    #block-menu-menu-demo li li a {
        color: #555 !important;
    }
    #block-menu-menu-demo li li a:active {
        color: #000 !important;
    }
    #block-menu-menu-demo li li:hover {
        background-color: #CCC !important;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="block-menu-menu-demo" class="block block-menu contextual-links-region">
    <h2>Family &amp; Community Menu</h2>
    <ul class="menu">
        <li class="first leaf"><a href="/fce">Main Page</a></li>
        <li class="leaf"><a href="/sde/families">For Families</a></li>
        <li class="leaf"><a href="/sde/schools">For Schools</a></li>
        <li class="leaf"><a href="/sde/communities">For Communities</a></li>
        <li class="last expanded"><a href="/sde/parent-community-engagement">Related Topics</a>
            <ul class="menu">
                <li class="first leaf"><a href="/sde/21cclc">21st CCLC</a></li>
                <li class="leaf"><a href="/sde/summer-learning">Summer Learning</a></li>
                <li class="last expanded"><a href="/sde/chronic-absenteeism">Chronic Absenteeism</a>
                    <ul class="menu">
                        <li class="first last leaf"><a href="/absenteeism-tool-kit">Toolkit</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>