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

I'm coding navbar in bootstrap and I've social media links added on the right side of the nav

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');
 });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744217788a4563634.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信