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

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

I have a html as sidebar, and use Bootstrap.

相关标签:
13条回答
  • 2020-11-30 05:35

    I am using bootstrap navigation

    This did the job for me including active main dropdown's and the active children

    $(document).ready(function () {
            var url = window.location;
        // Will only work if string in href matches with location
            $('ul.nav a[href="' + url + '"]').parent().addClass('active');
    
        // Will also work for relative and absolute hrefs
            $('ul.nav a').filter(function () {
                return this.href == url;
            }).parent().addClass('active').parent().parent().addClass('active');
        });
    
    0 讨论(0)
  • 2020-11-30 05:35
    $(".nav li").click(function() {
        if ($(".nav li").removeClass("active")) {
            $(this).removeClass("active");
        }
        $(this).addClass("active");
    });
    

    This is what I came up with. It checks if the "li" element has the class of active, if it doesn't it skips the remove class part. I'm a bit late to the party, but hope this helps. :)

    0 讨论(0)
  • 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

    <ul class="nav nav-list">
        <li id="tab1" class="active"><a href="/">Link 1</a></li>
        <li id="tab2"><a href="/link2">Link 2</a></li>
        <li id="tab3"><a href="/link3">Link 3</a></li>
    </ul>

    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.

    0 讨论(0)
  • 2020-11-30 05:38

    html code in my case

    <ul class="navs">
       <li  id="tab1"><a href="index-2.html">home</a></li>
       <li id="tab2"><a href="about.html">about</a></li>
    
       <li id="tab3"><a href="project-02.html">Products</a></li>
    
       <li id="tab4"><a href="contact.html">contact</a></li>
     </ul>
    

    and js code is

      $('.navs 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);
    }
    
    0 讨论(0)
  • 2020-11-30 05:44

    If you are using server side code like Java then you need to return active tab name and on return page have some function to disable( meaning remove active class) all tabs except the one you returned.

    This would require you to add id or name to each tab. Little bit more work :)

    0 讨论(0)
  • 2020-11-30 05:44

    <ul class="nav nav-list">
        <li id="tab1" class="active"><a href="/">Link 1</a></li>
        <li id="tab2"><a href="/link2">Link 2</a></li>
        <li id="tab3"><a href="/link3">Link 3</a></li>
    </ul>

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