user1721546 user1721546 - 5 months ago 21
CSS Question

Tab panels with maxcdn css dont work

This is basic JQuery to show and hide panels when a list item in the menu is clicked but I can't get my code to work.The CSS I am using is max cdn so I am not sure if I need to have my own CSS file for this.

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.min.css"></script>
<script src ="PhpProject1/jquery.js"></script>
<script>
$(document).ready(function() {
$('.tabs a').click(function(){
var $this =$(this);
$('.panel').hide();
$('.tabs a.active').removeClass('active');
$this.addClass('active').blur();});
}); // end ready
</script>
</head>
<body>
<div id="tabContainer">
<h2>Tab Heading</h2>
<div class=col-xs-3">
<ul class="nav nav-tabs" role="tablist">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Menu1</a></li>
<li><a href="#">Menu2</a></li>
<li><a href="#">Menu3</a></li>
</ul>
<div id ="panel1" class="panel panel-primary">
<div class="panel-heading">
#panel1
</div>
<div class="panel-body">content
</div>
</div>
<div id ="panel2" class="panel panel-primary">
<div class="panel-heading">
#panel2
</div>
<div class="panel-body">content
</div>
</div>
<div id ="panel3" class="panel panel-primary">
<div class="panel-heading">
#panel3
</div>
<div class="panel-body">content
</div>
</div>
</div>
</body>
</html>

Answer

The biggest issue is that you have a script tag pointing to CSS.

This:

<script src="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.min.css"></script>

should be this:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.css">

Even after fixing that, there are other issues too. E.g. $('.tabs a').click(... doesn't do anything, because there is no element on the page with the class "tabs". Changing this to $('.nav-tabs a').click(... is a good start, though I still think you have more work to do.

EDIT

Here's one way to fix things up. New script:

$(document).ready(function () {
    $('.nav-tabs li').click(function (e) {
        e.preventDefault();
        var $this = $(this);
        $('.panel').hide();
        $('#' + $this.find('a').attr('href')).show();
        $('.nav-tabs .active').removeClass('active');
        $this.addClass('active').blur();
        return false;
    });
}); // end ready

New tabs:

<ul class="nav nav-tabs" role="tablist">
    <li class="active"><a href="home">Home</a></li>
    <li><a href="panel1">Menu1</a></li>
    <li><a href="panel2">Menu2</a></li>
    <li><a href="panel3">Menu3</a></li>
</ul>

New panels:

<div id="home" class="panel panel-primary">
    <div class="panel-heading">
        #home
    </div>
    <div class="panel-body">
        Home content
    </div>
</div>
<div id ="panel1" class="panel panel-primary" style="display: none">
    <div class="panel-heading">   
        #panel1
    </div>
    <div class="panel-body">content
    </div>    
</div>
<div id ="panel2" class="panel panel-primary" style="display: none">
    <div class="panel-heading">   
        #panel2
    </div>
    <div class="panel-body">content
    </div>    
</div>
<div id ="panel3" class="panel panel-primary" style="display: none">
    <div class="panel-heading">   
        #panel3
    </div>
    <div class="panel-body">content
    </div>    
</div>

A couple things to note:

  1. I set up the click handler on the lis instead of the as, since this is where the "active" class lives.
  2. I used the href attribute on the anchor tags to decide which panel to display.
  3. I made a new "home" panel and set all the others to the initial style display:none.
Comments