How to keep active class when changing pages

后端 未结 3 1709
粉色の甜心
粉色の甜心 2021-01-22 23:34

I am trying to add an active class to nav item, depending what page your on. I am using this script:



        
相关标签:
3条回答
  • 2021-01-22 23:55

    Here is a possible solution: 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'). Use the same logic in other location.

    0 讨论(0)
  • 2021-01-23 00:06

    Using data elements and a delegate function: https://jsfiddle.net/ebo7hLo9/12/

    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

    $(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");
        }
    });
    
    0 讨论(0)
  • 2021-01-23 00:10

    https://jsfiddle.net/ebo7hLo9/10/ - here's a fiddle!

    $(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. 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. 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. That complex of code is largely unnecessary. I replaced it with the class selector $(".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.

    For more info take a look at this previous stackoverflow post: Storing Objects in HTML5 localStorage

    0 讨论(0)
提交回复
热议问题