如何使用jQuery将Bootstrap 4导航项添加到活动类

时间:2019-05-04 11:53:24

标签: javascript jquery bootstrap-4 jekyll

我有一个jekyll网站,为此,我想使用javascript动态添加类active bootstrap 4类。我用下面的代码。

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

代码位于footer.js中,已加载到源代码中。 但是它仅适用于主页here。请提出正确的代码。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试:

var pathname = window.location.pathname;

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