为什么 Bootstrap 折叠功能不起作用?

Why is Bootstrap collapse function not working?

<nav id="na-vrh" class="navbar navbar-expand-lg navbar navbar-dark bg-dark">            
   <div class="container-fluid">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
         <ul class="navbar-nav">
            <li class="nav-item">
               <a class="nav-link" href="#clanek1">Luka Dončić</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#clanek2">Goran Dragić</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#clanek03">Olimpija</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#dodaj-clanek">Dodaj nov članek</a>
            </li>
         </ul>
      </div>
   </div>
</nav>

所以我在实现 Botstrapp 汉堡包菜单时遇到了问题。问题是它不会崩溃……我包含了必需的 link 和脚本标签,但它仍然不起作用。我还删除了所有正文内容,并直接从 Bottstrap 站点复制并粘贴了整个导航栏,但仍然不起作用。

您的代码很好,但我认为您错误地导入了 Bootstrap 5 的脚本或样式。

您可以尝试 运行 以下代码段与您的导航栏完全集成。

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

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" />

  <title>Hello, world!</title>
</head>

<body>
  <nav id="na-vrh" class="navbar navbar-expand-lg navbar navbar-dark bg-dark">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="#clanek1">Luka Dončić</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#clanek2">Goran Dragić</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#clanek03">Olimpija</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#dodaj-clanek">Dodaj nov članek</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>
  <!-- Adding your Bootstrap 5's Javascript here -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</body>

</html>

如果你包含 bootstrap javascript.

就可以了

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">



<nav id="na-vrh" class="navbar navbar-expand-lg navbar navbar-dark bg-dark">            
   <div class="container-fluid">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
         <ul class="navbar-nav">
            <li class="nav-item">
               <a class="nav-link" href="#clanek1">Luka Dončić</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#clanek2">Goran Dragić</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#clanek03">Olimpija</a>
            </li>
            <li class="nav-item">
               <a class="nav-link" href="#dodaj-clanek">Dodaj nov članek</a>
            </li>
         </ul>
      </div>
   </div>
</nav>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>