使用maxcdn css的选项卡面板不起作用

时间:2016-06-18 20:36:44

标签: jquery html css

这是在单击菜单中的列表项时显示和隐藏面板的基本JQuery,但我无法使用我的代码。我使用的CSS是max cdn所以我不确定是否需要拥有我自己的CSS文件。

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

1 个答案:

答案 0 :(得分:1)

最大的问题是你有一个指向CSS的脚本标记。

此:

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

应该是这样的:

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

即使在确定之后,也存在其他问题。例如。 $('.tabs a').click(...没有做任何事情,因为页面上没有类别&#34;标签&#34;。将此更改为$('.nav-tabs a').click(...是一个良好的开端,但我仍然认为您还有更多工作要做。

修改

这是解决问题的一种方法。新脚本:

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

新标签:

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

新面板:

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

有几点需要注意:

  1. 我在li s而不是a s上设置了点击处理程序,因为这是&#34;活跃的&#34;上课。
  2. 我使用锚标记上的href属性来决定要显示哪个面板。
  3. 我做了一个新的&#34; home&#34;面板并将所有其他设置为初始样式display:none