根据url自动展开JQuery嵌套菜单

Auto expand JQuery nested menu depending on url

本文关键字:JQuery 嵌套 菜单 url 根据      更新时间:2023-09-26

我正在使用它在我的站点中获得可扩展菜单

        $(document).ready(function () {                
            $('li.category').addClass('plusimageapply');
            $('li.category').children().addClass('selectedimage');
            $('li.category').children(":not(a)").hide();
            $('li.category').each(
            function (column) {
                $(this).click(function (event) {
                    if (this == event.target) {
                        if ($(this).is('.plusimageapply')) {
                            $(this).children().show(200);
                            $(this).removeClass('plusimageapply');
                            $(this).addClass('minusimageapply');
                        }
                        else {
                            $(this).children(":not(a)").hide(200);
                            $(this).removeClass('minusimageapply');
                            $(this).addClass('plusimageapply');
                        }
                    }
                });
            }
            );
        });

如何使其自动扩展到当前菜单?我的链接采用格式

/Article/Category1/Category3

如果我在这个URL,我想扩展类别3,它在类别1下。菜单项的格式为

<li class="category">
    <a href"/Article/Category1/Category3/">Category3</a>
    <ul> ... [more sub-categories] ... </ul>
</li>

使用window.location.pathname将其与菜单的链接相匹配。

看看这个问题,了解如何使用jQuery:做到这一点

使用jQuery将当前URL与UL中的herf匹配,如果匹配,则添加类