oys*_*hiu 3 javascript wordpress jquery twitter-bootstrap bootstrap-4
我在Wordpress网站上设置了Bootstrap 4选项卡,并希望链接到另一个页面链接中的特定选项卡:
index.php文件:
<a href="<?php echo get_site_url(); ?>/services/#innovation">Discover More</a>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '-tab"]').tab('show');
} //add a suffix
// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
})
Run Code Online (Sandbox Code Playgroud)
我尝试过这个解决方案但没有成功: - Twitter Bootstrap选项卡:转到页面重新加载或超链接上的特定选项卡
谢谢
我会推荐这样的东西/使用pushState-否则您的浏览器会滚动到您的ID:
$(function() {
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
history.pushState({}, '', e.target.hash);
});
var hash = document.location.hash;
var prefix = "tab_";
if (hash) {
$('.nav-tabs a[href="'+hash.replace(prefix,"")+'"]').tab('show');
}
});
Run Code Online (Sandbox Code Playgroud)
它无法正常工作,因为您需要在调用页面加载之前附加shown.bs.tab处理程序..tab('show')
// wire up shown event
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
console.log("tab shown...");
});
// read hash from page load and change tab
var hash = document.location.hash;
var prefix = "tab_";
if (hash) {
$('.nav-tabs a[href="'+hash.replace(prefix,"")+'"]').tab('show');
}
Run Code Online (Sandbox Code Playgroud)
Bootstrap 4 - 页面加载时的活动选项卡
代码:http://www.codeply.com/go/Yu8wbjeGMZ
演示:http://www.codeply.com/render/Yu8wbjeGMZ#tab2