简体   繁体   English

引导导航活动链接不起作用

[英]bootstrap navigation active links not working

I'm trying to make my navigation active on the active div, the following example shows and hides the correct divs but doesn't add the active class to the link: 我试图使导航在活动div上处于活动状态,以下示例显示并隐藏了正确的div,但未将活动类添加到链接中:

HTML 的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 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
};

I think your problem lies within 我认为您的问题出在

($(this).prop("href")).show(); 

Because that is not a valid selector. 因为那不是有效的选择器。 If you are just trying to get the current link just reference it like this: 如果您只是想获取当前链接,请像这样引用它:

$(this).show(); 

There's two things I did... I commented out the prop href show line, and removed the .closest and I'm finding that active is now properly being added as a class to the a tag. 我做了两件事...我注释了prop href show行,并删除了.closest,发现现在可以将active作为类正确添加到a标签中。

Here's the fiddle I was using: 这是我正在使用的小提琴:

http://jsfiddle.net/LWAmE/ http://jsfiddle.net/LWAmE/

(I didn't add bootstrap as I didn't need to in order to test the code out) (我没有添加引导程序,因为我不需要添加引导程序来测试代码)

$("#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.

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