简体   繁体   English

更改页面时如何保持活动班级

[英]How to keep active class when changing pages

I am trying to add an active class to nav item, depending what page your on.我正在尝试向导航项目添加一个活动类,具体取决于您所在的页面。 I am using this script:我正在使用这个脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $("#side-bar a").click(function () {
            var id = $(this);

            $(id).siblings().find(".active").removeClass("active");
            $(id).addClass("active");
            localStorage.setItem("selectedolditem", id);
        });

        var selectedolditem = localStorage.getItem('selectedolditem');

        if (selectedolditem !== null) {
            $(selectedolditem).siblings().find(".active").removeClass("active");
            $(selectedolditem).addClass("active");
        }
    });
</script>

See full jsfiddle here: https://jsfiddle.net/ebo7hLo9/ It adds the active class, but it loads a new page, it disappears.在这里查看完整的 jsfiddle: https ://jsfiddle.net/ebo7hLo9/ 它添加了活动类,但它加载了一个新页面,它消失了。 What am I doing wrong?我究竟做错了什么?

https://jsfiddle.net/ebo7hLo9/10/ - here's a fiddle! https://jsfiddle.net/ebo7hLo9/10/ - 这是一个小提琴!

$(document).ready(function () {

            $("#side-bar a").click(function () {
                var id = $(this);

                $(".active").removeClass("active");
                $(id).addClass("active");
                localStorage.setItem("selectedolditem", $(id).text());

            });

            var selectedolditem = localStorage.getItem('selectedolditem');

            if (selectedolditem !== null) {

                $("a:contains('" + selectedolditem + "')").addClass("active");
            }
        });

Your code was having issues remembering what element to grab.您的代码在记住要抓取的元素时遇到问题。 I think it's due to the web storage API's unfamiliarity with objects.我认为这是由于 Web 存储 API 不熟悉对象。 Instead I got the text from the element that was selected, stored it in localStorage and on page load matched it with the correct element.相反,我从所选元素中获取文本,将其存储在 localStorage 中,并在页面加载时将其与正确的元素匹配。 Also there was part of your code that was dealing with finding the class "active" within the siblings() of the selected element and removing it.此外,您的部分代码正在处理在所选元素的siblings()中查找“活动”类并将其删除。 That complex of code is largely unnecessary.这种复杂的代码在很大程度上是不必要的。 I replaced it with the class selector $(".active")我用类选择器$(".active")替换了它

I didn't change this in the code, but I'd advise against using localStorage in favor of sessionStorage so that the storage will clear itself on tab/browser close.我没有在代码中更改这一点,但我建议不要使用localStorage来支持sessionStorage这样存储将在选项卡/浏览器关闭时自行清除。

For more info take a look at this previous stackoverflow post: Storing Objects in HTML5 localStorage有关更多信息,请查看之前的 stackoverflow 帖子: 在 HTML5 localStorage 中存储对象

Here is a possible solution: https://jsfiddle.net/6yyLpma1/这是一个可能的解决方案: https : //jsfiddle.net/6yyLpma1/

              $("#side-bar a").click(function () {
                    var id = $(this);

                    $('#side-bar').find(".active").removeClass("active");
                    $(id).addClass("active");
                    localStorage.setItem("selectedolditem", id);
                });

Instead of $(id).siblings() use $('#side-bar') .而不是$(id).siblings()使用$('#side-bar') Use the same logic in other location.在其他位置使用相同的逻辑。

Using data elements and a delegate function: https://jsfiddle.net/ebo7hLo9/12/使用数据元素和委托函数: https : //jsfiddle.net/ebo7hLo9/12/

HTML HTML

<span id="delegateAnchor">
    <div id="side-bar">
        <ul>
            <li><a href="#" data-desc="home">Home</a></li>
            <li><a href="#" data-desc="whoAreWe">Who we are</a></li>
            <li><a href="#" data-desc="services">Services</a></li>
            <li><a href="#" data-desc="whatToExpect">What to expect</a></li>
            <li><a href="#" data-desc="representClients">Representative clients</a></li>
            <li><a href="#" data-desc="successStories">Success stories</a></li>
            <li><a href="#" data-desc="currentLit">Current litigation</a></li>
            <li><a href="#" data-desc="whatIfYouCould">What if you could not be a doctor?</a></li>
        </ul>
    </div>
</span>

Javascript Javascript

$(document).ready(function () {
    $('#delegateAnchor').on('click', '#side-bar a', function() {
        var $this = $(this);
        var linkId = $this.data('desc');

        $this.closest('ul').find('a').removeClass("active");
        $this.addClass("active");

        localStorage.setItem("menuSelection", linkId);
    });

    var selectedLinkId = localStorage.getItem("menuSelection");

    if (selectedLinkId !== null) {
        $('#side-bar a[data-desc="'+ selectedLinkId +'"]').trigger("click");
    }
});

暂无
暂无

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

相关问题 使用javascript滚动时如何保持活动状态? - How to keep class active when scrolling with javascript? 更改页面时phonegap继续运行javascript - phonegap keep running javascript when changing pages 如何在 javascript 中重新加载时将活动 class 保存到本地存储并保留 - How to save active class to localstorage and keep when reloading in javascript 在 ASP.Net MVC 5 应用程序中访问页面时如何保持活动菜单项突出显示? - How to keep the active menu item highlighted when visiting pages in ASP.Net MVC 5 application? 我使用“数据目标”来保持活动链接样式。 即使从活动链接移动页面,如何保持样式? - I used “data-target” to keep the active link style. How to maintain style even when moving pages from an active link? 我使用“数据目标”来保持活动链接样式。 即使从活动链接移动页面,如何保持样式 - I used “data-target” to keep the active link style. How to maintain style even when moving pages from an active link 子页面处于活动状态时保持手风琴菜单打开 - Keep accordion menu open when sub-pages are active 相对div激活时jQuery更改类 - Jquery changing class when relative divs active 如果元素有一些子页面,如何添加活动类导航元素? - How to add active class to nav ul li a element when it has some sub pages with it? 打开(添加/编辑)页面时如何在框架导航菜单中添加活动类 - how to add active class in framework nav menu when (add/edit) pages are opened
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM