简体   繁体   中英

Bootstrap tabs. Add border to current tab through jquery

Here bootstrap tabs:

      <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li style="padding: 20px" role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
    <li style="padding: 20px" role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
    <li style="padding: 20px" role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
    <li style="padding: 20px" role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home">

        <h1>Hello World</h1>

    </div>
    <div role="tabpanel" class="tab-pane" id="profile">Hello</div>
    <div role="tabpanel" class="tab-pane" id="messages">Some text</div>
    <div role="tabpanel" class="tab-pane" id="settings">Second text</div>
  </div>

</div>
        </div>

and current tab has class active automatically with bootstrap javascript.

So i want i add border to current tab through jquery:

$('li').click(function () {
     if ($(this).hasClass('active')) {
         $(this).addClass('border-add')
     }
 })

I can not do it. Because this code works bad. If will click one of the tabs border will appear to the current tab. But if i will click to another tab the first i clicked tab border will not disappear. Guys explain me how to realize it. Thank you in advance!

This can be done without JQuery using the class

li.active{
 border: 1px solid black;
}

 /* Latest compiled and minified CSS included as External Resource*/ /* Optional theme */ @import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css'); body { margin: 10px; } li.active{ border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li style="padding: 20px" role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li> <li style="padding: 20px" role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li> <li style="padding: 20px" role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li> <li style="padding: 20px" role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li> </ul> <!-- Tab panes --> <div class="tab-content"> <div role="tabpanel" class="tab-pane active" id="home"> <h1>Hello World</h1> </div> <div role="tabpanel" class="tab-pane" id="profile">Hello</div> <div role="tabpanel" class="tab-pane" id="messages">Some text</div> <div role="tabpanel" class="tab-pane" id="settings">Second text</div> </div> </div> </div> 

You probably are just about there. Sounds like all you need to do is remove border-add before adding it:

$('li').click(function () {
  $('li.border-add').removeClass('border-add'); // remove from the one that has it first
  if ($(this).hasClass('active')) {
    $(this).addClass('border-add'); // then add the new one
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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