jQuery submenu like an accordion

2020-05-07 04:39发布

问题:

on the webpage oshadi-yoga.ch i like to get an navigation menu with a list like this:

<ul>
    <li class="section-title">Yoga
        <ul style="display: none;">
            <li><a href="/">Approach</a></li>
            <li><a href="/">Asanas</a></li>
            <li><a href="/">Yoga</a></li>
            <li><a href="/">Kirtan</a></li>
        </ul>
    </li>
</ul>

i wrote some jquery to get an accordion effect. if you click the first level the second list shall open with an toggle effect:

    $(function() {
        $("#lbar li.section-title ul").hide();
        $("#lbar li.section-title").click(function() {
            $(this).find("ul").toggle();
        });
    });

    $(function() {
        $("#lbar li.section-titleact ul").show();
        $("#lbar li.section-titleact").click(function() {
            $(this).find("ul").toggle();
        });
    });

    $(function() {
        $("#lbar li.section-titleact ul li a").click(function() {
            $("#lbar li.section-titleact ul").css("display", "block");
        });
    });

now the submenu is hidden when you open the page. this is correct. you click a menu-item and the submenu is shown. this is correct. there is no link to a page in the first level. then you click a link in the second level the page is opened, but the second level <ul> is hidden for a few seconds. this is the error.

unfortunately i'm not able to correct the jquery script. can someone help me or has an example of a menu i need?

回答1:

I should rewrite the code al little

<ul>
    <li class="section-title"><span>Yoga</span>
        <ul style="display: none;">
            <li><a href="/">Approach</a></li>
            <li><a href="/">Asanas</a></li>
            <li><a href="/">Yoga</a></li>
            <li><a href="/">Kirtan</a></li>
        </ul>
    </li>
</ul>

Added a span.

jQuery:

$('.section-title > span').on('click', function()
{
    $(this).siblings('ul').slidetoggle();
});

I hope this helps a little (or misunderstood I the question a bit?)