jQuery submenu like an accordion

本小妞迷上赌 提交于 2019-12-20 05:49:19

问题


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?)



来源:https://stackoverflow.com/questions/10250387/jquery-submenu-like-an-accordion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!