最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to make <li> add css class active after refreshing page? - Stack Overflow

programmeradmin3浏览0评论

after clicking li item. i set active attribute to css class but li has a link to forward another page. active css disappear? how to do this? i have been used localstorage to cache my old selected li item. i can not find any solution. How to make alive active class setting after refreshing? how to set active class alive after refreshing page?

<script type="text/javascript">

$(document).ready(function () {

    $("li").click(function () {
        var id = $(this).attr("id");
        console.log(id);

        var selectedolditem = localStorage.getItem('selectedolditem');
        if (selectedolditem != null) {

            $('#' + selectedolditem).siblings().find("active").removeClass("active");
            $('#' + selectedolditem).addClass("active");
            localStorage.clear();
            return;

        }

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

    });
});

after clicking li item. i set active attribute to css class but li has a link to forward another page. active css disappear? how to do this? i have been used localstorage to cache my old selected li item. i can not find any solution. How to make alive active class setting after refreshing? how to set active class alive after refreshing page?

<script type="text/javascript">

$(document).ready(function () {

    $("li").click(function () {
        var id = $(this).attr("id");
        console.log(id);

        var selectedolditem = localStorage.getItem('selectedolditem');
        if (selectedolditem != null) {

            $('#' + selectedolditem).siblings().find("active").removeClass("active");
            $('#' + selectedolditem).addClass("active");
            localStorage.clear();
            return;

        }

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

    });
});

Share Improve this question edited Apr 21, 2015 at 6:26 MrCode 64.5k10 gold badges92 silver badges113 bronze badges asked Jun 25, 2013 at 10:33 lokiloki 2,9669 gold badges66 silver badges121 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 11

The problem with your code is you are trying to retrieve it onclick. That's the wrong logic. The correct logic is: when the page loads, retrieve it. When the li is clicked, store it. You also have a problem when using find("active"), you need to use the class character . otherwise it will search for elements with the tag name active not the class.

$(document).ready(function () {
    $("li").click(function () {
        var id = $(this).attr("id");

        $('#' + id).siblings().find(".active").removeClass("active");
            //                       ^ you forgot this
        $('#' + id).addClass("active");
        localStorage.setItem("selectedolditem", id);
    });

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

    if (selectedolditem != null) {
        $('#' + selectedolditem).siblings().find(".active").removeClass("active");
        //                                        ^ you forgot this
        $('#' + selectedolditem).addClass("active");
    }
});

Try following.

$('li').each(function(){
    if(window.location.href.indexOf($(this).find('a:first').attr('href'))>-1)
    {
    $(this).addClass('active').siblings().removeClass('active');
    }
});

Try following.

$(document).ready(function() {
            $('.active').removeClass('active');
            var currurl = window.location.pathname;
            var val=$('li:has(a[href="'+currurl+'"])').addClass('active');
        });
发布评论

评论列表(0)

  1. 暂无评论