简体   繁体   中英

Bootstrap 3 nav-tabs won't change on click

So I just started a new site in bootstrap and I'm working on a nav-tabs design. I've followed the directions to the tee and have almost copied and pasted at this point just to get it to function but alas I have several problems:

(1) The nav-tabs do not change on click ie the url changes to my element id but the actual tab does not become "active" (Home remains depressed while the others stay unpressed)

(2) The tab-content div only displays the content marked by class="active".

I have included the necessary jquery and bootstrap js scripts and I believe they are in the correct order but no matter what I do these tabs never switch their active state to the one that is clicked.

Here is my html:

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery-1.11.2.min" type="text/javascript"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<title>New Homepage w/ Bootstrap</title>
</head>

<body>
    <div class="nav">
         <div class="container">
        <div class="tabbable">
    <ul class="nav nav-tabs" data-tabs="tabs" id="myTab">
    <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
    <li><a data-toggle="tab" href="#about">About</a></li>
    <li><a data-toggle="tab" href="#subscribe">Subscribe</a></li>
    <li><a data-toggle="tab" href="#search">Search</a></li>
    <li><a data-toggle="tab" href="#logout">Logout</a></li>
    </ul>
    <div class="tab-content">
    <div class="tab-pane active" id="home">
        <h1>Hospital Data Solutions</h1>
        <p>Financial Analysis Database</p>
    </div>
    <div class="tab-pane" id="about">
        <h1>About Us</h1>
        <p>Cost Reports and Financial Analysis Nationwide Database</p>
    </div>
    <div class="tab-pane" id="subscribe">
        <h1>Annual Subscription</h1>
        <p>Purchase an annual subscription to access all cost reports.</p>
    <h2>Individual Cost Reports</h2>
    <p>Purchase individual cost reports directly from us in .pdf, .xs, .csv formats.</p>
    </div>
    <div class="tab-pane" id="search">
        <h1>Search our database</h1>
        <p>Search content goes here.</p>
    </div>
    <div class="tab-pane" id="logout">
        <h1>logout</h1>
        <p>Logout fx goes here.</p>
    </div>
            </div>
        </div>  
    </div>
</div>
<script src="js/bootstrap.js"></script>
</body>
</html>

I have attempted the various solutions I could find including the addition of

   <script>
    $(document).ready(function () {

            $('#myTab a').click(function (e) {
                e.preventDefault()
                $(this).tab('show')
            })

            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                e.relatedTarget // previous tab
            });
        });
</script>

Underneath the bootstrap.min.js script but like I said just about anything I've done has produced NO change whatsoever. Everything else looks fine on the page but a little guidance here would truly get me started on the right foot. I haven't been working with bootstrap for more than a few hours so forgive my ignorance. The docs say that bootstrap does not even require javascript to toggle the tabs when data-toggle is used so I'm quite confused on what the right answer is.

:EDIT: The first answer was correct. Placing the tags right before the closing body tag did not improve functionality but replacing my scripts with

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

            $('#myTab a').click(function (e) {
                e.preventDefault()
                $(this).tab('show')
            })

            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                e.relatedTarget // previous tab
            });
        });
</script>

Did indeed work. I my initial jquery and bootstrap files were in the correct place but it seems they are not completely functional/broken in some way or something. Thanks again!

By the way: Am I able to link to these repositories constantly or will I have to update my pages when new updates of bootstrap/jquery come out? What I'm asking is will these repositories will be deleted?

The tabs are working fine as can be seen here: https://jsfiddle.net/AndrewL32/nh6ma48r/

<script type="text/javascript">
    $(document).ready(function () {

            $('#myTab a').click(function (e) {
                e.preventDefault()
                $(this).tab('show')
            })

            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                e.relatedTarget // previous tab
            });
        });
</script>

You need to keep bootstrap.js as well as the above script that calls the function above on your <head></head> section but below your jQuery.

Also, please replace your:

<script src="bootstrap.js"></script> with this <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

and your

<script src="jQuery"></script> with this <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

(issue could be because your js files are edited, moved or missing)

The only thing I can think of (but what usually causes this issue) is your <link> and <script> tags are in the wrong spot. Copying your code to an editor of mine and adding jquery.min.js and bootstrap.min.js right before the closing </body> tag caused it to work just fine for me. Here is the HTML file:

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href=".../bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <title>New Homepage w/ Bootstrap</title>
    </head>

    <body>
        <div class="nav">
            <div class="container">
                <div class="tabbable">
                    <ul class="nav nav-tabs" data-tabs="tabs" id="myTab">
                        <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
                        <li><a data-toggle="tab" href="#about">About</a></li>
                        <li><a data-toggle="tab" href="#subscribe">Subscribe</a></li>
                        <li><a data-toggle="tab" href="#search">Search</a></li>
                        <li><a data-toggle="tab" href="#logout">Logout</a></li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="home">
                            <h1>Hospital Data Solutions</h1>
                            <p>Financial Analysis Database</p>
                        </div>
                        <div class="tab-pane" id="about">
                            <h1>About Us</h1>
                            <p>Cost Reports and Financial Analysis Nationwide Database</p>
                        </div>
                        <div class="tab-pane" id="subscribe">
                            <h1>Annual Subscription</h1>
                            <p>Purchase an annual subscription to access all cost reports.</p>
                            <h2>Individual Cost Reports</h2>
                            <p>Purchase individual cost reports directly from us in .pdf, .xs, .csv formats.</p>
                        </div>
                        <div class="tab-pane" id="search">
                            <h1>Search our database</h1>
                            <p>Search content goes here.</p>
                        </div>
                        <div class="tab-pane" id="logout">
                            <h1>logout</h1>
                            <p>Logout fx goes here.</p>
                        </div>
                    </div>
                </div>  
            </div>
        </div>
        <script src=".../jquery.min.js" type="text/javascript"></script>
        <script src=".../bootstrap.min.js" type="text/javascript"></script>
    </body>
</html>

Note: Replace .../ with the correct URL to bootstrap.min.css , jquery.min.js and bootstrap.min.js . Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM