最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove active class from bootstrap navbar links - Stack Overflow

programmeradmin10浏览0评论

I'm coding navbar in bootstrap and I've social media links added on the right side of the navbar, if links are clicked they are opened in a new window. I want to make so social media links would not get active class on a click.

<ul class="nav navbar-nav navbar-right">
    <li id="facebook">
        <a href="#" target="_blank"><i class="fa fa-lg fa-facebook"></i></a>
    </li>  
    <li id="twitter">
        <a href="#"  target="_blank"><i class="fa fa-lg fa-twitter"></i></a>
    </li> 
    <li id "google-plus">
        <a href="#"  target="_blank"><i class="fa fa-lg fa-google-plus"></i></a>
    </li> 
</ul>

This is what I tried, but it doesn't work and I don't know why? link1, would be default active link for that page.

<script>
    $(".nav li").click(function() {
        $(".nav li#link1").addClass('active');
        $(".nav li#facebook").removeClass('active');
        $(".nav li#twitter").removeClass('active');
        $(".nav li#google-plus").removeClass('active');
    });
</script>

I'm coding navbar in bootstrap and I've social media links added on the right side of the navbar, if links are clicked they are opened in a new window. I want to make so social media links would not get active class on a click.

<ul class="nav navbar-nav navbar-right">
    <li id="facebook">
        <a href="#" target="_blank"><i class="fa fa-lg fa-facebook"></i></a>
    </li>  
    <li id="twitter">
        <a href="#"  target="_blank"><i class="fa fa-lg fa-twitter"></i></a>
    </li> 
    <li id "google-plus">
        <a href="#"  target="_blank"><i class="fa fa-lg fa-google-plus"></i></a>
    </li> 
</ul>

This is what I tried, but it doesn't work and I don't know why? link1, would be default active link for that page.

<script>
    $(".nav li").click(function() {
        $(".nav li#link1").addClass('active');
        $(".nav li#facebook").removeClass('active');
        $(".nav li#twitter").removeClass('active');
        $(".nav li#google-plus").removeClass('active');
    });
</script>
Share Improve this question edited Oct 5, 2015 at 10:42 Krupesh Kotecha 2,4123 gold badges22 silver badges40 bronze badges asked Oct 5, 2015 at 10:28 user2821023user2821023 5111 gold badge8 silver badges22 bronze badges 3
  • can u create fiddle link? – Arun Commented Oct 5, 2015 at 10:30
  • jsfiddle/DTcHh/12790 – user2821023 Commented Oct 5, 2015 at 10:46
  • could be simplified to $(".nav li").click(function() { //add class to link and $(this).removeClass('active')}); – Johan Commented Oct 5, 2015 at 10:58
Add a ment  | 

4 Answers 4

Reset to default 5

try this

<script>
  $(function(){

    $('.navbar-right li').click(function(){ 
      $(this).addClass('active').siblings().removeClass('active');
    }); 
  })
</script>

SEE DEMO

Try This

<script>
    $(".nav li").click(function() {
       $(".nav li").removeClass('active');
       $(this).addClass('active');
    });
</script>

You can achieve it in CSS like,

#navbar ul li#facebook a:hover,#navbar ul li#facebook a:active {
    color: #3b5998 !important;
}

Try this-

$(".nav li").click(function() {

   $(".nav li a:active").css('display',null);// discard inline style
   $(".nav li a:active").removeClass('active');//remove pseudo class
   $(".nav li#link1").addClass('active');
 });
发布评论

评论列表(0)

  1. 暂无评论