为什么引导程序下拉菜单不起作用?

时间:2021-05-24 14:19:37

标签: bootstrap-4

这是我使用引导程序的 html 模板。但是下拉菜单在 chrome 中不起作用,但在其他平台(例如 codeplay)中有效。

这是html代码

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <h1 class="navbar-brand">Portfolio</h1>
  <ul class="navbar-nav ml-auto">
    <li class="nav-item"><a class="nav-link" href="#">Home</a></li>
    <li class="nav-item"><a class="nav-link" href="">About Me</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Contact Me</a></li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                   Projects
                   </a>
      <div class="dropdown-menu bg-dark" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="">Action</a>
        <a class="dropdown-item" href="">Another action</a>
        <div class="dropdown-divider"></div>
        <a class="dropdown-item" href="#">Something else here</a>
      </div>
    </li>
  </ul>
</nav>

2 个答案:

答案 0 :(得分:0)

您应该在文件底部添加一个 JQuery CDN:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

答案 1 :(得分:0)

您在结束正文标记之前缺少 bootstrap.js。
https://getbootstrap.com/docs/4.6/getting-started/introduction/#quick-start

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <h1 class="navbar-brand">Portfolio</h1>
  <ul class="navbar-nav ml-auto">
    <li class="nav-item"><a class="nav-link" href="#">Home</a></li>
    <li class="nav-item"><a class="nav-link" href="">About Me</a></li>
    <li class="nav-item"><a class="nav-link" href="#">Contact Me</a></li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                   Projects
                   </a>
      <div class="dropdown-menu bg-dark" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="">Action</a>
        <a class="dropdown-item" href="">Another action</a>
        <div class="dropdown-divider"></div>
        <a class="dropdown-item" href="#">Something else here</a>
      </div>
    </li>
  </ul>
</nav>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>