[英]How to add bootstrap 4 nav item to a class active using jquery
我有一個jekyll網站,為此,我想使用javascript動態添加class 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
中。 但是它只在這里工作 。 請提出正確的代碼。 謝謝。
嘗試這個:
var pathname = window.location.pathname;
$('.navbar-nav > li > a[href="/' + pathname.replace(/\//g,'') + '"]').addClass('active');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.