折叠后,Bootstrap导航栏无法切换打开状态

时间:2018-03-24 17:15:33

标签: jquery html twitter-bootstrap navbar jquery-mobile-navbar

我检查了所有解决方案。我复制了很多导航栏示例代码。我的导航栏应该会崩溃,但点击该按钮不会切换或展开。

在头部我引用了CSS for bootstrap

    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">

在结尾正文标记之前的页面底部,我正在引用.js文件。

    <script type="text/javascript" src="framework7/js/framework7.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/news-feed.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script defer src="js/fontawesome-all.js"></script>
    <script type="text/javascript" src="js/jquery.3.3.1.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>

然后这是我从bootswatch或bootstrap获得的导航代码,不记得我已经尝试了很多这样的例子。

    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <a class="navbar-brand" href="#">Catholic Cemeteries</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarColor01">
            <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Features</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#">Pricing</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">About</a>
           </li>
           </ul>

        </div>
      </nav>

先谢谢,约翰

2 个答案:

答案 0 :(得分:1)

您需要popper.js。首先是jquery.js,然后是popper.js,然后是bootstrap.js ......

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ></script>

并确保浏览器控制台中没有JS错误。

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/poppe.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ></script>

   <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <a class="navbar-brand" href="#">Catholic Cemeteries</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarColor01">
        <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
           <a class="nav-link" href="#">Pricing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
       </li>
       </ul>
    </div>
   </nav>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您应该重新排序脚本标记。应该在自己的脚本文件之前添加jQuery,bootstrap和其他第三方文件,因为您在自己的代码中使用第三方工具,因此需要在加载自己的脚本之前加载它们。

我假设index.jsnews-feed.js是您自己的JS文件。所以我把它们放在底部,而(根据我的假设)第三方JS文件放在最上面。

<script type="text/javascript" src="framework7/js/framework7.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script defer src="js/fontawesome-all.js"></script>
<script type="text/javascript" src="js/jquery.3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/news-feed.js"></script>

如果news-feed.js中使用的内容为index.js,则news-feed.js之前的脚本标记为index.js。我希望这有助于@ZimSystem的回答