我在我的网页上使用Bootstrap 4,我遇到导航标签的问题。
我需要通过超链接从登录标签内容页面打开忘记密码标签。
下面的代码在Bootstrap 3中为我工作,但在Bootstrap 4
中没有<ul class="nav nav-tabs" role="tablist">
<li class="nav-item"><a class="nav-link active" href="#log-in" data-toggle="tab">Log in</a></li>
<li class="nav-item"><a class="nav-link" href="#forgot-password" data-toggle="tab">Forgot password</a></li>
<li class="nav-item"><a class="nav-link" href="#sign-up" data-toggle="tab">Sign up</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="log-in">
Login tab
<a href="#forgot-password" data-toggle="tab">Go to Forget Password</a>
</div>
<div class="tab-pane fade" id="forgot-password">Forgt password tab</div>
<div class="tab-pane fade" id="sign-up">sign-up tab</div>
</div>
答案 0 :(得分:5)
我可以考虑两种方法来解决这个问题:
#hash
es进行标签导航,因此简单的javascript可以在常规链接上收听点击事件,并且触发额外点击 - 在相应的标签。#hash
es并根据该值的更改打开标签。这种方法还具有以下优点:标签可以直接链接,因此您可以使用例如example.com #sign-up打开一个打开特定标签的页面。下面您将找到每种方法的两个片段。
<强> 1。在引擎盖下点击:
$('.tab-link').on('click', function(event) {
// Prevent url change
event.preventDefault();
// `this` is the clicked <a> tag
$('[data-toggle="tab"][href="' + this.hash + '"]').trigger('click');
})
&#13;
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="log-in-tab" data-toggle="tab" href="#log-in" role="tab" aria-controls="log-in" aria-selected="true">Log in</a>
</li>
<li class="nav-item">
<a class="nav-link" id="forgot-password-tab" data-toggle="tab" href="#forgot-password" role="tab" aria-controls="forgot-password" aria-selected="false">Forgot password</a>
</li>
<li class="nav-item">
<a class="nav-link" id="sign-up-tab" data-toggle="tab" href="#sign-up" role="tab" aria-controls="sign-up" aria-selected="false">Sign up</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="log-in" role="tabpanel" aria-labelledby="log-in-tab">
Login tab<br />
<a href="#forgot-password" Xdata-toggle="tab" class="tab-link">Go to Forget Password</a>
</div>
<div class="tab-pane fade" id="forgot-password" role="tabpanel" aria-labelledby="forgot-password-tab">
Forgt password tab
</div>
<div class="tab-pane fade" id="sign-up" role="tabpanel" aria-labelledby="sign-up-tab">
Sign-up tab
</div>
</div>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
&#13;
<强> 2。在网址中使用哈希:
$(document).ready(function() {
function onHashChange() {
var hash = window.location.hash;
if (hash) {
// using ES6 template string syntax
$(`[data-toggle="tab"][href="${hash}"]`).trigger('click');
}
}
window.addEventListener('hashchange', onHashChange, false);
onHashChange();
});
&#13;
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="log-in-tab" data-toggle="tab" href="#log-in" role="tab" aria-controls="log-in" aria-selected="true">Log in</a>
</li>
<li class="nav-item">
<a class="nav-link" id="forgot-password-tab" data-toggle="tab" href="#forgot-password" role="tab" aria-controls="forgot-password" aria-selected="false">Forgot password</a>
</li>
<li class="nav-item">
<a class="nav-link" id="sign-up-tab" data-toggle="tab" href="#sign-up" role="tab" aria-controls="sign-up" aria-selected="false">Sign up</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="log-in" role="tabpanel" aria-labelledby="log-in-tab">
Login tab<br />
<a href="#forgot-password" Xdata-toggle="tab" class="tab-link">Go to Forget Password</a>
</div>
<div class="tab-pane fade" id="forgot-password" role="tabpanel" aria-labelledby="forgot-password-tab">
Forgt password tab
</div>
<div class="tab-pane fade" id="sign-up" role="tabpanel" aria-labelledby="sign-up-tab">
Sign-up tab
</div>
</div>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
&#13;
答案 1 :(得分:2)
这可以通过触发一个在单击链接时更改选项卡的函数来实现。
<强> HTML 强>
<ul class="nav nav-tabs" role="tablist" id="myTabs">
<li class="nav-item">
<a class="nav-link active" href="#log-in" data-toggle="tab">Log in</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#forgot-password" data-toggle="tab">Forgot password</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#sign-up" data-toggle="tab">Sign up</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="log-in">
Login tab
<a href="#forgot-password" data-toggle="tab" class="forgot-password-link">Go to Forget Password</a>
</div>
<div class="tab-pane fade" id="forgot-password">Forgt password tab</div>
<div class="tab-pane fade" id="sign-up">sign-up tab</div>
</div>
<强>的Javascript 强>
$( ".forgot-password-link" ).click(function() {
$('#myTabs li:nth-child(2) a').tab('show')
});
Codepen示例:https://codepen.io/Washable/pen/VQYewy