简体   繁体   中英

How to add bootstrap 4 nav item to a class active using jquery

I have a jekyll site, to this I want to add class active bootstrap 4 class dynamically using javascript. I used below code.

$(document).ready(function() {
    // get current URL path and assign 'active' class
    var pathname = window.location.pathname;
    $('.navbar-nav > li > a[href="'+pathname+'"]').addClass('active');
})

code is in footer.js loaded in source. But it only working for home page here . Please suggest correct code. Thanks.

try this:

var pathname = window.location.pathname;

$('.navbar-nav > li > a[href="/' + pathname.replace(/\//g,'') + '"]').addClass('active');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM