Bootstrap导航栏没有扩展

时间:2015-12-02 13:56:34

标签: javascript jquery html css twitter-bootstrap

我正在尝试使用Bootstrap创建一个可折叠的导航栏,但无论我尝试多少次,多少次检查完成的方式,我都无法点击按钮展开导航栏。

这是我的HTML:

<section id="upper-section">
    <div class="container mini-container">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-target="#nav" 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 href="index.html" class="navbar-brand">
                        <img src="img/logo.png" alt="Logo">
                    </a>
                </div>
                <div id="nav" class="navbar-collapse collapse navbar-right">
                    <ul class="nav navbar-nav">
                        <li class="active">
                            <a href="index.html">Home</a>
                        </li>
                        <li>
                            <a href="prjects.html">Projects</a>
                        </li>
                        <li>
                            <a href="professors.html">Professors</a>
                        </li>
                        <li>
                            <a href="contact.html">Contact</a>
                        </li>
                        <li>
                            <a href="admin.html">Administration</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
</section>

我确保在jquery之前加载bootstrap.js

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

我不知道什么是错的,为什么按下按钮不会扩展导航栏?

修改: fiddle

4 个答案:

答案 0 :(得分:2)

它缺少按钮上的数据切换

               <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav" 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>

答案 1 :(得分:0)

我在这里遇到了同样的问题。我从boostrap网站上获取了3个js CDN(jquery,popper.js和js):
https://getbootstrap.com/docs/4.3/getting-started/introduction/
将这些链接放在html的头部。我已经将引导css cdn放在了那些下面。

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

请点击网站链接以获取最新版本。

答案 2 :(得分:-1)

您需要在li元素中使用下拉类,并在ul元素中使用下拉菜单:

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Directorio MINSAL</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Exposiciones<span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Exposiciones 1</a></li>
                        <li><a href="#">Exposiciones 2</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Asambleas<span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Asambleas 1</a></li>
                        <li><a href="#">Asambleas 2</a></li>
                    </ul>
                <li><a href="#">Estudiantes</a></li>
                <li><a href="#">Maestros</a></li>
                <li><a href="#">Formación pedágogica</a></li>
                <li class="active"><a href="#">Item Activo</a></li>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="/login/final">Cerrar Sesión</a></li>
            </ul>
        </div>
    </div>
</div>

......和F12。在这些情况下它非常有用。

答案 3 :(得分:-1)

最近,我也遇到了同样的情况,我发现目标ID(即id =“ nav”)的名称中有空格...就像下面一样

<div id="nav " class="navbar-collapse collapse navbar-right">

因此,删除了多余的空白后,它对我有用。 我知道我的答案与发布的问题无关...如果有任何读者觉得有用的话,我会写它。