Bootstrap 4导航栏不会折叠或展开

时间:2018-02-15 21:38:13

标签: html css twitter-bootstrap bootstrap-4

我正在自学网络开发,我认为比开始自己的个人网页更好的方法。

我遇到了使用Bootstrap 4和导航栏的问题。

我可以获取列表,并在窗口缩小时显示图标,但我无法扩展或切换崩溃。我甚至复制并粘贴了一些来自bootstrap文档的代码,看看我做错了什么,即使这样也行不通。

我已尝试使用firefox和chrome,而且两者都有,我仍然无法使用图标按钮切换崩溃。

以下是代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="description" content="Heath's Personal Website">
  <meta name="keywords" content="heath, blandford, heath blandford">
  <meta name="author" content="heath blandford">
  <meta name="viewport" content="width=device-width, initial-scale=1">



  <!--import bootstrap -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <!-- import google fonts lobster and open sans -->
  <link href="https://fonts.googleapis.com/css?family=Lobster|Open+Sans" rel="stylesheet">



</head>

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

  <div class="collapse navbar-collapse" id="navbarTop">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item"><a class="nav-link" href="#about">About</a> </li>
      <li class="nav-item"><a class="nav-link" href="#portfolio">Services</a> </li>
      <li class="nav-item"><a class="nav-link" href="#contact">Contact</a> </li>
      <li class="nav-item"><a class="nav-link" href="#social">Social Media</a> </li>
    </ul>
  </div>
</nav>

<body>

</body>

</html>

1 个答案:

答案 0 :(得分:9)

虽然您已导入Bootstrap CSS,但您忽略了导入所需的JavaScript。

导入Bootstrap的JavaScript和所需的依赖项( jQuery Popper.JS )将导致您的导航切换正确。

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

这可以在以下内容中看到:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="description" content="Heath's Personal Website">
  <meta name="keywords" content="heath, blandford, heath blandford">
  <meta name="author" content="heath blandford">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!--import bootstrap -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  
  <!-- import google fonts lobster and open sans -->
  <link href="https://fonts.googleapis.com/css?family=Lobster|Open+Sans" rel="stylesheet">

</head>

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

  <div class="collapse navbar-collapse" id="navbarTop">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item"><a class="nav-link" href="#about">About</a> </li>
      <li class="nav-item"><a class="nav-link" href="#portfolio">Services</a> </li>
      <li class="nav-item"><a class="nav-link" href="#contact">Contact</a> </li>
      <li class="nav-item"><a class="nav-link" href="#social">Social Media</a> </li>
    </ul>
  </div>
</nav>

<body>

</body>

</html>