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

前端 未结 13 1277
刺人心
刺人心 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');
        });
    

提交回复
热议问题