How to change active class while click to another link in bootstrap use jquery?

前端 未结 13 1265
刺人心
刺人心 2020-11-30 05:32

I have a html as sidebar, and use Bootstrap.

13条回答
  •  情书的邮戳
    2020-11-30 05:38

    You can use cookies for save postback data from one page to another page.After spending a lot time finally i was able to make this happen. I am suggesting my answer to you(this is fully working since I also needed this).

    first give your li tag to valid id name like

    After that copy and paste this script. since I have written some javascript for reading, deleting and creating cookies.

     $('.nav li a').click(function (e) {
      
            var $parent = $(this).parent();
            document.cookie = eraseCookie("tab");
            document.cookie = createCookie("tab", $parent.attr('id'),0);
     });
    
        $().ready(function () {
            var $activeTab = readCookie("tab");
            if (!$activeTab =="") {
            $('#tab1').removeClass('ActiveTab');
              }
           // alert($activeTab.toString());
           
            $('#'+$activeTab).addClass('active');
        });
    
    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
    
        document.cookie = name + "=" + value + expires + "; path=/";
    }
    
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    
    function eraseCookie(name) {
        createCookie(name, "", -1);
    }

    Note: Make sure you have implmeted according to your requirement. I have written this script according to my implementation and its working fine for me.

提交回复
热议问题