admin管理员组

文章数量:1302887

In bootstrap, I am trying to learn how to switch tabs. This is what I got so far

 <!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=".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();
    });

The problem is I am sorta hacking bootstrap to make tabs with in my JS code. I don't think this is the right way to do it, but I can't find any good examples of how to switch tabs. Like in my html, I have inline styles to hide the divs for the tabs that don't show initially.

Does anyone know how to do it?

Thanks

EDIT this worked for me

 <!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=".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> 

In bootstrap, I am trying to learn how to switch tabs. This is what I got so far

 <!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./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();
    });

The problem is I am sorta hacking bootstrap to make tabs with in my JS code. I don't think this is the right way to do it, but I can't find any good examples of how to switch tabs. Like in my html, I have inline styles to hide the divs for the tabs that don't show initially.

Does anyone know how to do it?

Thanks

EDIT this worked for me

 <!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./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> 
Share Improve this question edited Jan 30, 2016 at 23:17 omega asked Jan 30, 2016 at 21:19 omegaomega 44k90 gold badges285 silver badges521 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

It works without any additional js. Just use pure bootstrap and this HTML markup:

<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>

本文标签: javascriptHow to properly switch tabs in bootstrapStack Overflow