[英]bootstrap navigation active links not working
我試圖使導航在活動div上處於活動狀態,以下示例顯示並隱藏了正確的div,但未將活動類添加到鏈接中:
的HTML
<div class="list-group" id="admin-list">
<a href="#add-user" data-toggle="tab" class="list-group-item"> Add a user </a>
</div>
JS
$("#nav li a").on("click", navigation("#nav li"));
$("#admin-list a").on("click", navigation("#admin-list a"));
var foo = function(navigation) {
$(navigation).removeClass("active"); //Remove any previously "active" li
$("#home, #about, #contact", "#add-user").hide(); //Hide all "pages"
$($(this).prop("href")).show(); //Show only the current target
$(this).addClass("active"); //Set click a as active
};
我認為您的問題出在
($(this).prop("href")).show();
因為那不是有效的選擇器。 如果您只是想獲取當前鏈接,請像這樣引用它:
$(this).show();
我做了兩件事...我注釋了prop href show行,並刪除了.closest,發現現在可以將active作為類正確添加到a標簽中。
這是我正在使用的小提琴:
(我沒有添加引導程序,因為我不需要添加引導程序來測試代碼)
$("#admin-list a").on("click", function(f){
f.preventDefault();
$("#admin-list a").removeClass("active"); //Remove any previously "active" a
$("#home, #about, #contact, #add-user").hide(); //Hide all "pages"
//$($(this).prop("href")).show(); //Show only the current target
$(this).addClass("active"); //Set click a as active
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.