简体   繁体   中英

Changing Tab pane tabs programmatically in bootstrap

I am using tab panes defined as

    <ul class="nav nav-tabs">
                   <li><a href="#personal" data-toggle="tab">Personal Information</a></li>
                   <li class="active"><a href="#contact"   data-toggle="tab">Contact</a></li>
                   <li><a href="#guardian"  data-toggle="tab">Parent/Guardian Information</a></li>
                   <li><a href="#education" data-toggle="tab">Educational Background</a></li>
                   <li><a href="#recommendations" data-toggle="tab">Study Prospects</a></li>
                </ul>


<div class="tab-content">                

                <div class="tab-pane" id="personal"></div>
                <div class="active tab-pane" id="contact"></div>
                <div class="tab-pane" id="guardian"></div>       
</div>

It can be seen that i have selected Contact as First selected Tab, however when I refresh the page< on full page load it automatically changes tab to Personal that is First tab.

Is there any way i can manually switch tabs via javascript etc?

there is a solution to change the tabs , so you will need to

1- keep the last tab some where

2- call the below function when the page refresh

this how to call it

activaTab('tab1');


function activaTab(tab){
  $('.nav-tabs a[href="#' + tab + '"]').tab('show');
};

This is the code you need

Html:

<li id="contact"><a href="#contact"   data-toggle="tab">Contact</a></li>

Javascript:

window.onload = function() {
  document.getElementById('contact').classList.add('active'); 
};

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