javascript - Bootstrap 4 - Link to specific tab -
i have bootstrap 4 tabs set in wordpress website , want link specific tab page link:
index.php:
<a href="<?php echo get_site_url(); ?>/services/#innovation">discover more</a>
services.php:
<!-- nav tabs --> <ul class="nav nav-tabs text-xs-center" role="tablist"> <li class="nav-item"> <a class="nav-link active" data-toggle="tab" href="#innovation" role="tab" aria-expanded="true"> <h4>innovation &<br/>investigation</h4> </a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="tab" href="#electronic-engineering" role="tab" aria-expanded="false"> <h4>electronic<br/>engineering</h4> </a> </li> <li class="nav-item"> <a class="nav-link manufacturing" data-toggle="tab" href="#manufacturing" role="tab" aria-expanded="false"> <h4>manufacturing</h4> </a> </li> </ul> <!-- tab panes --> <div class="tab-content clearfix"> <!-- innovation --> <div class="tab-pane active" id="innovation" role="tabpanel" aria-expanded="true"> <div data-aos="fade" data-aos-duration="2000" class="col-sm-5"> content </div> </div> <!-- electronic engineering --> <div data-aos="fade" data-aos-duration="2000" class="tab-pane" id="electronic-engineering" role="tabpanel" aria-expanded="false"> <div class="col-sm-5"> content </div> </div> <!-- manufacturing --> <div data-aos="fade" data-aos-duration="2000" class="tab-pane" id="manufacturing" role="tabpanel" aria-expanded="false"> <div class="col-sm-5"> content </div> </div> </div>
javascript:
// javascript enable link tab var url = document.location.tostring(); if (url.match('#')) { $('.nav-tabs a[href="#' + url.split('#')[1] + '-tab"]').tab('show'); } //add suffix // change hash page-reload $('.nav-tabs a').on('shown.bs.tab', function (e) { window.location.hash = e.target.hash; })
i had tried solutions without success: - twitter bootstrap tabs: go specific tab on page reload or hyperlink
thanks
it's not working because need attach shown.bs.tab
handler before calling .tab('show')
on page load.
// wire shown event $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { console.log("tab shown..."); }); // read hash page load , change tab var hash = document.location.hash; var prefix = "tab_"; if (hash) { $('.nav-tabs a[href="'+hash.replace(prefix,"")+'"]').tab('show'); }
bootstrap 4 - active tab on page load
code: http://www.codeply.com/go/yu8wbjegmz
demo: http://www.codeply.com/render/yu8wbjegmz#tab2
Comments
Post a Comment