美好的一天,
我正在创建带有表单的引导程序选项卡(BS4)的页面。
现在我需要有一个前进和后退按钮形式的导航,并且我想使用BS4的tab('show')功能。
但是,即使在概念验证中,我也无法使其起作用。 我想知道我做错了什么。
我检查是否所有ID都已设置,并且所有内容都匹配。到目前为止,一切都是正确的。控制台日志不会给我任何错误或任何提示。
我希望我犯了一个简单的错误,这不是引导问题。
在我的概念证明代码下面:
我的 functions.js 文件的内容:
function activaTab(tab) {
var tabID = '#' + tab;
console.log('You just clicked on a function to activate tab : ' + tabID);
if ($(tabID).length == 0) {
console.log('But I have to inform you of the misfortunate event that the tab ID you want to pick strangely does not exist');
}
$('.nav-tabs a[href="' + tabID + '"]').tab('show'); // Select tab by name
};
我的HTML文件的内容:
function activaTab(tab) {
var tabID = '#' + tab;
console.log('You just clicked on a function to activate tab : ' + tabID);
if ($(tabID).length == 0) {
console.log('But I have to inform you of the misfortunate event that the tab ID you want to pick strangely does not exist');
}
$('.nav-tabs a[href="' + tabID + '"]').tab('show'); // Select tab by name
};
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
</li>
</ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">..home.<br />
<button class="btn btn-lg btn-primary" onclick="activaTab('pills-profile');">go to profile</button>
</div>
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">..profile.<br />
<button class="btn btn-lg btn-primary" onclick="activaTab('pills-contact');">go to contact</button>
</div>
<div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">..contact.</div>
</div>
</div>
<!DOCTYPE html>
<html>
<head>
<title>activate tab</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="functions.js"></script>
</head>
<body>
<div class="container">
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
</li>
</ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">..home.<br />
<button class="btn btn-lg btn-primary" onclick="activaTab('pills-profile');">go to profile</button>
</div>
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">..profile.<br />
<button class="btn btn-lg btn-primary" onclick="activaTab('pills-contact');">go to contact</button>
</div>
<div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">..contact.</div>
</div>
</div>
</body>
</html>
编辑:
一个很疲倦的头脑造成了这个。
$('。nav-tabs a [href =“'+ tabID +'”]')。tab('show'); //按名称选择标签
要在中进行更改:
$('。nav-pills a [href =“'+ tabID +'”]')。tab('show'); //按名称选择标签
答案 0 :(得分:1)
在activaTab()方法中,您要对具有类名称 nav-tabs 的元素调用 tab('show'),但是您希望对该元素进行调用显示器上的类为 nav-pills 。正确的呼叫应为:
$('.nav-pills a[href="' + tabID + '"]').tab('show'); // Select tab by name