首页 > 解决方案 > 使用 bootstrap 4 时,bootstrap 下拉按钮不会下拉

问题描述

我正在使用引导程序创建一个烧瓶应用程序。我正在尝试使用需要 jquery + popper 的元素,例如下拉菜单。按钮出现了,但没有下拉

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="/static/node_modules/bootstrap/dist/css/bootstrap.min.css">

    <title>hello world</title>
  </head>

   <div class="dropdown">
   <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown button
   </button>
   <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
   </div>
   </div>

    <script src="/static/node_modules/jquery/dist/jquery.min.js"</script>
    <script src="/static/node_modules/popper.js/dist/popper.min.js" </script>
    <script src="/static/node_modules/bootstrap/dist/js/bootstrap.min.js" </script>
  <body>
</html>

作为参考,这是我的文件树的样子

在此处输入图像描述

这些是我安装的依赖项的版本(使用 yarn 安装)

  1. @popperjs/core@2.4.4
  2. 引导@4.5.2
  3. jquery@3.3.1
  4. popper.js@1.16.1

控制台中没有显示错误消息

当我像这样引用脚本时,我没有任何问题

https://getbootstrap.com/docs/4.1/getting-started/introduction/

<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.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

我已经看到了很多相关的问题,但是到目前为止所建议的解决方案都没有奏效。这是我迄今为止看到的建议:

  1. 确保 jquery 脚本出现在引导脚本之前(检查)
  2. 删除 popper 依赖并使用 bootstrap.bundle.js 或 bootstrap.bundle.min.js (我都试过了)
  3. 确保您没有两次包含引导 jquery(检查)
  4. 添加 <script>$('.dropdown-toggle').dropdown();</script>(无效)

标签: htmljquerybootstrap-4drop-down-menupopper.js

解决方案


您忘记关闭 index.html 中的脚本标签

<script src="/static/node_modules/jquery/dist/jquery.min.js"></script>
<script src="/static/node_modules/popper.js/dist/popper.min.js"></script>
<script src="/static/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>

推荐阅读