【发布时间】:2021-10-15 09:45:47
【问题描述】:
所以我使用 Django 来提供网页服务。
-
我正在努力确保我能够下载所有必要的资源,以使网页按预期的方式运行以供离线使用。其中之一是引导程序 5。
-
我正在尝试使用他们的下拉菜单功能...根据他们的文档:
"Dropdowns are built on a third party library, Popper, which provides dynamic positioning and viewport detection. Be sure to include popper.min.js before Bootstrap’s JavaScript or use bootstrap.bundle.min.js / bootstrap.bundle.js which contains Popper. Popper isn’t used to position dropdowns in navbars though as dynamic positioning isn’t required." -
因为我非常渴望找到解决方案,所以我插入了一堆
<script>标签试图排除问题......也许这是个坏主意。
说了这么多,我还没弄明白。帮助:)?
请告诉我如何改进这个问题。提前谢谢!
{% load static %}
<html class="w-100 h-100">
<head>
<title>
Fonts
</title>
<!--Important meta tags-->
<!--Cancel zoom for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<!--example:-->
<!--href="{% static 'polls/style.css' %}"-->
<!--theme-->
<link rel="shortcut icon" type="image/jpg" href="{% static 'Pages/favicons/CES.png' %}"/>
<link type="text/css" rel="stylesheet" href="{% static 'Pages/css/main.css' %}">
<script src="{% static 'Pages/js/main.js' %}"></script>
<!--jQuery-->
<script src="{% static 'Pages/jQuery/jquery-3.6.0.js' %}"></script>
<script src="{% static 'Pages/jQuery/jquery-3.6.0.min.js' %}"></script>
<script src="{% static 'Pages/jQuery/jquery-3.6.0.slim.js' %}"></script>
<script src="{% static 'Pages/jQuery/jquery-3.6.0.slim.min.js' %}"></script>
<!--bootstrap related-->
<link type="text/css" rel="stylesheet" href="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/css/bootstrap.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/css/bootstrap.min.css' %}">
<script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.bundle.js' %}"></script>
<script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.bundle.min.js' %}"></script>
<script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.js' %}"></script>
<script src="{% static 'Pages/Bootstrap/bootstrap-5.0.2-dist/js/bootstrap.min.js' %}"></script>
<style>
{% if fonts %}
{% for font in fonts %}
@font-face
{
{% if font.font_family %}
font-family: "{{font.font_family}}";
{% endif %}
{% if font.path %}
src: url("{{font.path}}");
{% endif %}
}
{% endfor %}
{% endif %}
</style>
</head>
<body class="m-0 p-0 w-100 h-100">
<button id="fonts" class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Fonts</span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="fonts">
{% if fonts%}
{% for font in fonts %}
<li><div class="dropdown-item" style="font-family: '{{font.font_family}}'; text-align: center;">{{font.font_name}}</div></li>
{% endfor %}
{% endif %}
</ul>
</body>
</html>
进一步阅读(来自http://5.9.10.113/65685854/dropdown-button-in-bootstrap-5-not-working)表明,实际上是新版本的 Popper 导致了问题......所以......我将进行更多调查,看看我能做些什么。
编辑答案:
仅出于测试目的,我从 bootstrap 的下载页面添加了这个 sn-p,它按预期工作。从这里我浏览了我所有的 bootstrap 资源(更不用说我折叠并添加了 Popper CDN),然后发现我的 bootstrap.js 文件有问题。从 npm 获取它的最新发行版(并将构建放入子目录供我使用)后,我也通过 npm 将 Popper CDN 交换为对应的(我下载了它,进入 node_modules/@popper/core/... ,找到最小文件并引用它)现在它就像一个魅力......最后值得一提的是:只使用 .MIN. 文件!* 如此流行。 min.js、bootstrap.min.js、bootstrap.min.css...在script 标签中包含所有其他文件将导致引导程序无法正常运行。
<html class="w-100 h-100">
<head>
<title>
Menu
</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</head>
<body class="m-0 p-0 w-100 h-100">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</body>
</html>
【问题讨论】:
-
插入所有这些脚本标签是完全错误的。您应该导入 Bootstrap CSS 文件和 Bootstrap JS 文件(Bundle)Only,它应该可以工作,我还要注意 Bootstrap 5 don't need jQuery to work.
-
是的。我必须开始。我认为我的 bootstrap.js 文件有问题 - 我不知道它可能已经以某种方式损坏了。它现在可以工作(使用您提到的设置)。很高兴了解 jquery,但我想我会保留它 - 使用它可能比使用干净的 javascript 更容易。
-
凯文的评论应被视为答案。为了在 bootstrap 5 的情况下进行澄清,您必须删除 popper 和 jQuery 并使用 bootstrap.bundle.js。