jQuery Bootstrap无法正常运行

时间:2016-06-06 17:23:16

标签: javascript jquery html css twitter-bootstrap

对不起,如果之前已经回答过,请联系我,如果有的话,我找不到任何东西..

基本上,我的jQuery在Bootstrap上运行良好,直到我制作了一个favicon。现在我认为我的脚本/头标签对齐是关闭的,所以我试着修复过去一小时,但没有任何工作。

同样将脚本放在正文末端,就像正常一样,不起作用。不知道我在这里做错了什么。

    <html lang="en"><head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title></title>

    <!-- jQuery -->
    <script src="js/jquery-1.12.4.min.js"></script>

    <!-- CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- Fonts -->
    <link href='https://fonts.googleapis.com/css?family=PT+Serif:400,700' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Exo+2:400,300,700,900,800' rel='stylesheet' type='text/css'>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

 <!-- snip html -->

    <!-- jQuery Version -->
    <script src="js/jquery-1.12.4.min.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>




</body></html>

我知道它现在在那里两次,但都不起作用。怎么了? :(

编辑:下拉列表不起作用,这是下拉列表的HTML:

        <!-- Navigation -->
 <nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><img src="img/logo/logo.png" class="img-responsive img-icon" /></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-default">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-right" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
我为按钮更改了一些CSS,这是问题吗?

/* Navbar Colours - Fixing Dropdown */

.nav > li a {
    color: #e97117;
}

.nav > li a:hover, a:focus {
    color: #e97117;
}

/* Navbar link Colours */

.navbar-default a:hover, a:focus {
    color: #e97117 !important;
}

/* Button Colour */

.btn {
    color: #fff;
    font-family: 'Exo 2', sans-serif;
}

.btn-default:hover,
.btn-default:focus {
    color: #e97117;
    background-color: transparent;
    border-color: #e97117;
}

.btn-secondary {
    color: #fff;
    background-color: transparent;
    border-color: #fff;
}

.btn-secondary:hover,
.btn-secondary:focus {
    color: #fff;
    background-color: #e97117;
    border-color: #fff;
}

6 个答案:

答案 0 :(得分:3)

正如@jdelaney所说,你错过了一个密钥ID。

<div class="collapse navbar-collapse navbar-default" id="bs-example-navbar-collapse-1">

此外,在您的代码示例中,您的js和css的路径是&#34; page relative&#34;而不是&#34;网站相对&#34;或者&#34;完全合格&#34;。因此,如果您要测试的页面不是您的&#34;主页&#34;您很可能根本不加载这些资产。您是否碰巧在控制台中看到404错误?

无论如何,这个例子似乎对@jdelaney建议的编辑起作用。

&#13;
&#13;
.nav > li a {
    color: #e97117;
}

.nav > li a:hover, a:focus {
    color: #e97117;
}

/* Navbar link Colours */

.navbar-default a:hover, a:focus {
    color: #e97117 !important;
}

/* Button Colour */

.btn {
    color: #fff;
    font-family: 'Exo 2', sans-serif;
}

.btn-default:hover,
.btn-default:focus {
    color: #e97117;
    background-color: transparent;
    border-color: #e97117;
}

.btn-secondary {
    color: #fff;
    background-color: transparent;
    border-color: #fff;
}

.btn-secondary:hover,
.btn-secondary:focus {
    color: #fff;
    background-color: #e97117;
    border-color: #fff;
}
&#13;
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">
        <img src="img/logo/logo.png" class="img-responsive img-icon" />
      </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-default" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a>
        </li>
        <li><a href="#">Link</a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a>
            </li>
            <li><a href="#">Another action</a>
            </li>
            <li><a href="#">Something else here</a>
            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a>
            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a>
            </li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-right" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

查看定义折叠按钮的行:

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">

注意它是如何data-target的。这指的是要崩溃的元素。如果您查看example on the Bootstrap site,您会注意到主要导航栏内容div具有该ID(您的HTML丢失):

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

答案 2 :(得分:0)

&#13;
&#13;
<!DOCTYPE html>
   <html lang="en"><head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title></title>

    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <!-- CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Fonts -->
    <link href='https://fonts.googleapis.com/css?family=PT+Serif:400,700' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Exo+2:400,300,700,900,800' rel='stylesheet' type='text/css'>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>
	
	<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
          </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-default">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-right" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>



</body></html>
&#13;
&#13;
&#13;

我要尝试的第一件事就是在头脑中:

变化

<script src="js/jquery-1.12.4.min.js"></script>

<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>

答案 3 :(得分:0)

我认为当你包含两次JQuery时会出现问题。我可以在您的代码中看到您在两个地方引用return B(1-a); 。删除<script src="js/jquery-1.12.4.min.js"></script>标记中的一个&amp;再试一次。

答案 4 :(得分:0)

我尝试了你的示例html和css代码,我只更改了jquery和bootstrap的包含(从部分删除了jquery并包含了来自cdn的jquery和bootstrap),你可以看到它没有任何问题。

仔细检查你的jquery和bootstrap是否正确包含(文件路径/版本冲突?)。

&#13;
&#13;
    /* Navbar Colours - Fixing Dropdown */

    .nav > li a {
        color: #e97117;
    }

    .nav > li a:hover,
    a:focus {
        color: #e97117;
    }


    /* Navbar link Colours */

    .navbar-default a:hover,
    a:focus {
        color: #e97117 !important;
    }


    /* Button Colour */

    .btn {
        color: #fff;
        font-family: 'Exo 2', sans-serif;
    }

    .btn-default:hover,
    .btn-default:focus {
        color: #e97117;
        background-color: transparent;
        border-color: #e97117;
    }

    .btn-secondary {
        color: #fff;
        background-color: transparent;
        border-color: #fff;
    }

    .btn-secondary:hover,
    .btn-secondary:focus {
        color: #fff;
        background-color: #e97117;
        border-color: #fff;
    }
&#13;
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title></title>

    <!-- CSS -->
    <link  href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Fonts -->
    <link href='//fonts.googleapis.com/css?family=PT+Serif:400,700' rel='stylesheet' type='text/css'>
    <link href='//fonts.googleapis.com/css?family=Exo+2:400,300,700,900,800' rel='stylesheet' type='text/css'>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
    <body>

        <!-- Navigation -->
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#"><img src="img/logo/logo.png" class="img-responsive img-icon" /></a>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse navbar-default">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
                        <li><a href="#">Link</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something else here</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="#">Separated link</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="#">One more separated link</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form class="navbar-form navbar-right" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search">
                        </div>
                        <button type="submit" class="btn btn-default">Submit</button>
                    </form>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container-fluid -->
        </nav>

        <!-- jQuery Version -->
        <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    </body>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

问题出在CSS中。

我添加了一个溢出:隐藏的函数,我读过它,因为我的隧道视图是jQuery。

J Delaney帮助我找到了它,现在一切都已修好了。

感谢所有试图帮助我的人!没有完整的代码你就不可能知道,但我仍然非常感谢你的帮助!