How to keep active class when changing pages

后端 未结 3 1708
粉色の甜心
粉色の甜心 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.

提交回复
热议问题