Bootstrap CSS Active Navigation

前端 未结 13 2355

On the Bootstrap website the subnav matches up with the sections and changes background color as you or scroll to the section. I wanted to create my own menu without all the

13条回答
  •  臣服心动
    2020-11-27 12:41

    I tried some of the top answers above, it works for ".active" class, but the links not working well, it still stays on the current page. Then I tried this:

    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');
    

    I found it here: Twitter Bootstrap add active class to li

提交回复
热议问题