Bootstrap nav tabs: active class doesnt work

2019-09-16 16:26发布

问题:

Here's the HTML code:

<div class="process">
      <h2 style="text-transform: uppercase;">The Application Process</h2>
      <br><br>
      <div class="row">
        <div class="col-md-3" align="left">
          <ul class="nav nav-pills flex-column" id="myTab" role="tablist">
            <li class="nav-item">
              <a class="nav-link active" data-toggle="tab" href="#home" role="tab" aria-controls="home">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" data-toggle="tab" href="#profile" role="tab" aria-controls="profile">Profile</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" data-toggle="tab" href="#messages" role="tab" aria-controls="messages">Messages</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" data-toggle="tab" href="#settings" role="tab" aria-controls="settings">Settings</a>
            </li>
          </ul>              
        </div>
        <div class="col-md-9">
          <div class="tab-content">
            <div class="tab-pane active" id="home" role="tabpanel">a</div>
            <div class="tab-pane" id="profile" role="tabpanel">b</div>
            <div class="tab-pane" id="messages" role="tabpanel">c</div>
            <div class="tab-pane" id="settings" role="tabpanel">d</div>
          </div>
        </div>
      </div>
    </div>

And here's the JS code:

<script>
  $(function () {
    $('#myTab a:last').tab('show')
  })
  $('ul.nav-pills li a').click(function (e) {
    $('ul.nav-pills li.active').removeClass('active')
    $(this).parent('li').addClass('active')
  })
</script>

The nav that have the active class is the home nav, but somehow the settings is the one who is active. I tried to search many times, but doesn't find the answer.

Is it the javascript or the container class? Because I already use container before the div "process". Thanks.

回答1:

If you add the bootstrap JS and get rid of your JS code, it should work:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<div class="process">
      <h2 style="text-transform: uppercase;">The Application Process</h2>
      <br><br>
      <div class="row">
        <div class="col-md-3" align="left">
          <ul class="nav nav-pills flex-column" id="myTab" role="tablist">
            <li class="nav-item active">
              <a class="nav-link" data-toggle="tab" href="#home" role="tab" aria-controls="home">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" data-toggle="tab" href="#profile" role="tab" aria-controls="profile">Profile</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" data-toggle="tab" href="#messages" role="tab" aria-controls="messages">Messages</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" data-toggle="tab" href="#settings" role="tab" aria-controls="settings">Settings</a>
            </li>
          </ul>              
        </div>
        <div class="col-md-9">
          <div class="tab-content">
            <div class="tab-pane active" id="home" role="tabpanel">a</div>
            <div class="tab-pane" id="profile" role="tabpanel">b</div>
            <div class="tab-pane" id="messages" role="tabpanel">c</div>
            <div class="tab-pane" id="settings" role="tabpanel">d</div>
          </div>
        </div>
      </div>
    </div>

Hope this helps. :)