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

javascript - Keep active tab on page refresh in bootstrap 4 using local storage? - Stack Overflow

programmeradmin3浏览0评论

I am trying to keep selected tab active on page refresh. but when i am not able to find any solution for tabs in bootstrap 4. i tried to make changes according to bootstrap 3 solutions but nothing work. please help me.

HTML

<ul class="nav my-nav1 nav-tabs mt-3 " id="myTab" role="tablist">
   <li class="nav-item border border-secondary rounded">
    <a class="nav-link active"  data-toggle="tab" href="#menu1">MENU1</a>
 </li>
 <li class="nav-item border border-secondary rounded">
   <a class="nav-link "  data-toggle="tab" href="#menu2">MENU2</a>
 </li>
</ul>

this is js i am using but it dosen't work.

JS

<script type="text/javascript">
  $(document).ready(function(){
  $('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
    localStorage.setItem('activeTab', $(e.target).attr('href'));
  });
  var activeTab = localStorage.getItem('activeTab');
  if(activeTab){
    $('#myTab a[href="' + activeTab + '"]').tab('show');
  }
 });
</script> 

I am trying to keep selected tab active on page refresh. but when i am not able to find any solution for tabs in bootstrap 4. i tried to make changes according to bootstrap 3 solutions but nothing work. please help me.

HTML

<ul class="nav my-nav1 nav-tabs mt-3 " id="myTab" role="tablist">
   <li class="nav-item border border-secondary rounded">
    <a class="nav-link active"  data-toggle="tab" href="#menu1">MENU1</a>
 </li>
 <li class="nav-item border border-secondary rounded">
   <a class="nav-link "  data-toggle="tab" href="#menu2">MENU2</a>
 </li>
</ul>

this is js i am using but it dosen't work.

JS

<script type="text/javascript">
  $(document).ready(function(){
  $('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
    localStorage.setItem('activeTab', $(e.target).attr('href'));
  });
  var activeTab = localStorage.getItem('activeTab');
  if(activeTab){
    $('#myTab a[href="' + activeTab + '"]').tab('show');
  }
 });
</script> 
Share Improve this question edited May 19, 2018 at 15:14 Carol Skelly 362k91 gold badges735 silver badges646 bronze badges asked May 19, 2018 at 8:05 VinayVinay 2,3394 gold badges21 silver badges34 bronze badges 5
  • 1 You can make use of localStorage – Ankit Agarwal Commented May 19, 2018 at 8:06
  • Possible duplicate of stackoverflow.com/questions/18999501/… – Nandita Sharma Commented May 19, 2018 at 8:06
  • 1 I tried solutions on stackoverflow but all solutions working with bootstrap 3 but when i tried with bootstrap 4 it dosen't work. – Vinay Commented May 19, 2018 at 8:08
  • please someone give me solution for above code. – Vinay Commented May 19, 2018 at 8:11
  • Possible duplicate of Bootstrap 3: Keep selected tab on page refresh – Riyafa Abdul Hameed Commented May 19, 2018 at 8:22
Add a comment  | 

1 Answer 1

Reset to default 21

The jQuery selector is wrong: $('#myTab a[href="' + activeTab + '"]'), it should be...

$('.nav-tabs a[href="' + activeTab + '"]').tab('show');

https://www.codeply.com/go/C2B3mcrgSJ

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    localStorage.setItem('activeTab', $(e.target).attr('href'));
});

var activeTab = localStorage.getItem('activeTab');
if(activeTab){
    $('.nav-tabs a[href="' + activeTab + '"]').tab('show');
}
发布评论

评论列表(0)

  1. 暂无评论