Bootstrap Dropdown Not Un-collapsing Menu

时间:2017-03-02 06:42:11

标签: javascript jquery html css twitter-bootstrap

我到处寻找,但似乎无法找到解决方案的答案。我正在关注如何为我的网站制作导航栏的教程,我也试图让它适合移动设备。我希望导航栏显示一个按钮,在较小的设备上取消折叠菜单。但即使在我添加jQuery之后,它仍然没有打开菜单。

这是我的代码,希望它只是一个我忽略的简单修复。



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Personal Website</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/main.css">


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">       </script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<body>

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <!-- Logo -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="mainNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand">Test</a>
        </div>

        <!-- Menu Items -->
        <div class="collapse navbar-collapse" id="mainNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a> </li>
                <li><a href="#">About</a> </li>
                <li><a href="#">Contact</a> </li>

                <!-- Drop Down Menu -->
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">My Profile <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">About</a> </li>
                        <li><a href="#">Contact</a> </li>
                    </ul>
                </li>
            </ul>

            <!-- Right Align -->
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Logout</a> </li>
            </ul>
        </div>
    </div>
</nav>
</body>

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

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

仅更新 data-target="mainNavbar" 替换为 data-target="#mainNavbar"

仅更新一行

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mainNavbar">

<meta charset="UTF-8">
<title>Personal Website</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/main.css">


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">       </script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <!-- Logo -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"  data-target="#mainNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand">Test</a>
        </div>

        <!-- Menu Items -->
        <div class="collapse navbar-collapse" id="mainNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a> </li>
                <li><a href="#">About</a> </li>
                <li><a href="#">Contact</a> </li>

                <!-- Drop Down Menu -->
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">My Profile <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">About</a> </li>
                        <li><a href="#">Contact</a> </li>
                    </ul>
                </li>
            </ul>

            <!-- Right Align -->
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Logout</a> </li>
            </ul>
        </div>
    </div>
</nav>

答案 1 :(得分:0)

按钮出错。您忘记将#放在数据目标属性中。

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#mainNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>