在bootstrap中,我正在尝试学习如何切换标签。这是我到目前为止所得到的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pal</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<!--
<input id="test"/>
<input id="test2"/>
-->
<div id="myTabs" class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">Pal</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</div>
</div>
</div>
<!-- Tab panes -->
<div id="myTabPanes" class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">qqq</div>
<div role="tabpanel" class="tab-pane" id="about" style="display:none;">ggg</div>
<div role="tabpanel" class="tab-pane" id="contact" style="display:none;">jjj</div>
</div>
<script src="https://code.jquery.com/jquery-3.0.0-beta1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/mvc.js"></script>
<script src="js/main.js"></script>
</body>
</html>
JS
$('#myTabs li').on('click', function (e) {
e.preventDefault();
$(this).siblings("li.active").removeClass("active");
$(this).addClass("active");
$("#myTabPanes > div:visible").hide();
$($(this).find("a").attr("href")).show();
});
$('#myTabs a.brand').on('click', function (e) {
$('#myTabs .nav li').eq(0).click();
});
问题是我在我的JS代码中使用hack bootstrap来制作标签。我不认为这是正确的方法,但我找不到任何关于如何切换标签的好例子。就像我的html一样,我有内联样式来隐藏最初没有显示的选项卡的div。
有谁知道怎么做?
由于
编辑这对我有用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pal</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<!--
<input id="test"/>
<input id="test2"/>
-->
<div id="myTabs" class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#tab-01" aria-controls="tab-01" data-toggle="tab" onclick="document.querySelector('#myTabs li.active').className = '';">Pal</a>
<div class="nav-collapse collapse">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation"><a href="#tab-02" aria-controls="tab-02" role="tab" data-toggle="tab">Tab 01</a></li>
<li role="presentation"><a href="#tab-03" aria-controls="tab-03" role="tab" data-toggle="tab">Tab 02</a></li>
<li role="presentation"><a href="#tab-04" aria-controls="tab-04" role="tab" data-toggle="tab">Tab 03</a></li>
</ul>
</div>
</div>
</div>
</div>
<!-- Tab panes -->
<div id="myTabPanes" class="tab-content">
<div role="tabpanel" class="tab-pane active" id="tab-01">A</div>
<div role="tabpanel" class="tab-pane" id="tab-02">B</div>
<div role="tabpanel" class="tab-pane" id="tab-03">C</div>
<div role="tabpanel" class="tab-pane" id="tab-04">D</div>
</div>
<script src="https://code.jquery.com/jquery-3.0.0-beta1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/mvc.js"></script>
<script src="js/main.js"></script>
</body>
</html>
答案 0 :(得分:4)
它没有任何额外的js。只需使用纯引导程序和此HTML标记:
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#tab-01" aria-controls="tab-01" role="tab" data-toggle="tab">Tab 01</a></li>
<li role="presentation"><a href="#tab-02" aria-controls="tab-02" role="tab" data-toggle="tab">Tab 02</a></li>
<li role="presentation"><a href="#tab-03" aria-controls="tab-03" role="tab" data-toggle="tab">Tab 03</a></li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="tab-01">
content of tab
</div>
<div role="tabpanel" class="tab-pane" id="tab-02">
content of tab
</div>
<div role="tabpanel" class="tab-pane" id="tab-03">
content of tab
</div>
</div>