Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap tabs not working: when I click on them nothing happens

I am trying to implement Twitter Bootstrap tabs in the following code, but it does not seem to work. The tabs get displayed, but when I click on them nothing happens. Below is the only code I am using. All the other CSS/JS scripts are copied from Twitter Bootstrap framework.

<!DOCTYPE html> <html lang="en"> <head> <!-- Le styles --> <link href="assets/twitterbootstrap/css/bootstrap.css" rel="stylesheet"> <link href="assets/twitterbootstrap/css/bootstrap-responsive.css" rel="stylesheet">  </head>  <body data-spy="scroll" data-target=".subnav" data-offset="50">  <div class="container">  <!--------> <div id="content">     <ul class="nav nav-tabs" data-tabs="tabs">         <li class="active"><a href="#red">Red</a></li>         <li><a href="#orange">Orange</a></li>         <li><a href="#yellow">Yellow</a></li>         <li><a href="#green">Green</a></li>         <li><a href="#blue">Blue</a></li>     </ul>     <div id="my-tab-content" class="tab-content">         <div class="tab-pane active" id="red">             <h1>Red</h1>             <p>red red red red red red</p>         </div>         <div class="tab-pane" id="orange">             <h1>Orange</h1>             <p>orange orange orange orange orange</p>         </div>         <div class="tab-pane" id="yellow">             <h1>Yellow</h1>             <p>yellow yellow yellow yellow yellow</p>         </div>         <div class="tab-pane" id="green">             <h1>Green</h1>             <p>green green green green green</p>         </div>         <div class="tab-pane" id="blue">             <h1>Blue</h1>             <p>blue blue blue blue blue</p>         </div>     </div> </div>  <script type="text/javascript">     jQuery(document).ready(function ($) {         $(".tabs").tabs();     }); </script>     </div> <!-- container -->  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js"></script> <script type="text/javascript" src="assets/twitterbootstrap/js/bootstrap.js"></script> </body> </html> 
like image 768
DEREK N Avatar asked Mar 09 '12 07:03

DEREK N


People also ask

What does twitter bootstrap do?

Twitter Bootstrap is a front end framework to develop web apps and sites fast. In modern web development, there are several components which are required in almost all web projects. Bootstrap provides you with all those basic modules - Grid, Typography, Tables, Forms, Buttons, and Responsiveness.

What is twitter bootstrap and why would a developer use it?

Twitter Bootstrap was originally named as Twitter Blueprint before it was released as an open-source project on GitHub on August 19th, 2011. Bootstrap was created as an internal tool with the aim of eliminating inconsistencies among developers using different tools.


2 Answers

You need to add tabs plugin to your code

<script type="text/javascript" src="assets/twitterbootstrap/js/bootstrap-tab.js"></script> 

Well, it didn't work. I made some tests and it started working when:

  1. moved (updated to 1.7) jQuery script to <head> section
  2. added data-toggle="tab" to links and id for <ul> tab element
  3. changed $(".tabs").tabs(); to $("#tabs").tab();
  4. and some other things that shouldn't matter

Here's a code

<!DOCTYPE html> <html lang="en"> <head> <!-- Le styles --> <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> </head>  <body>  <div class="container">  <!--------> <div id="content">     <ul id="tabs" class="nav nav-tabs" data-tabs="tabs">         <li class="active"><a href="#red" data-toggle="tab">Red</a></li>         <li><a href="#orange" data-toggle="tab">Orange</a></li>         <li><a href="#yellow" data-toggle="tab">Yellow</a></li>         <li><a href="#green" data-toggle="tab">Green</a></li>         <li><a href="#blue" data-toggle="tab">Blue</a></li>     </ul>     <div id="my-tab-content" class="tab-content">         <div class="tab-pane active" id="red">             <h1>Red</h1>             <p>red red red red red red</p>         </div>         <div class="tab-pane" id="orange">             <h1>Orange</h1>             <p>orange orange orange orange orange</p>         </div>         <div class="tab-pane" id="yellow">             <h1>Yellow</h1>             <p>yellow yellow yellow yellow yellow</p>         </div>         <div class="tab-pane" id="green">             <h1>Green</h1>             <p>green green green green green</p>         </div>         <div class="tab-pane" id="blue">             <h1>Blue</h1>             <p>blue blue blue blue blue</p>         </div>     </div> </div>   <script type="text/javascript">     jQuery(document).ready(function ($) {         $('#tabs').tab();     }); </script>     </div> <!-- container -->   <script type="text/javascript" src="../bootstrap/js/bootstrap.js"></script>  </body> </html> 
like image 96
BartekR Avatar answered Oct 31 '22 22:10

BartekR


The key elements include:

  1. the class="nav nav-tabs" and data-tabs="tabs" of ul
  2. the data-toggle="tab" and href="#orange" of the a
  3. the class="tab-content" of the div of the content
  4. the class="tab-pane" and id of the div of the items

The complete code is as below:

<ul class="nav nav-tabs" data-tabs="tabs">     <li class="active"><a data-toggle="tab" href="#red">Red</a></li>     <li><a data-toggle="tab" href="#orange">Orange</a></li>     <li><a data-toggle="tab" href="#yellow">Yellow</a></li>     <li><a data-toggle="tab" href="#green">Green</a></li>     <li><a data-toggle="tab" href="#blue">Blue</a></li> </ul> <div class="tab-content">     <div class="tab-pane active" id="red">         <h1>Red</h1>         <p>red red red red red red</p>     </div>     <div class="tab-pane" id="orange">         <h1>Orange</h1>         <p>orange orange orange orange orange</p>     </div>     <div class="tab-pane" id="yellow">         <h1>Yellow</h1>         <p>yellow yellow yellow yellow yellow</p>     </div>     <div class="tab-pane" id="green">         <h1>Green</h1>         <p>green green green green green</p>     </div>     <div class="tab-pane" id="blue">         <h1>Blue</h1>         <p>blue blue blue blue blue</p>     </div> </div> 
like image 24
Jacky Avatar answered Nov 01 '22 00:11

Jacky