是因为Django使用了模板引擎来生成HTML页面,而模板引擎会对HTML代码进行处理,可能会导致Modal无法正常工作。解决这个问题的方法有两种:
{% load static %}
<link rel="stylesheet" href="{% static 'django_modal.css' %}">
<script src="{% static 'django_modal.js' %}"></script>
<button data-toggle="modal" data-target="#myModal">打开模态框</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="myModalLabel">模态框标题</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
模态框内容
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap.css' %}">
<script src="{% static 'jquery.js' %}"></script>
<script src="{% static 'bootstrap.js' %}"></script>
<button data-toggle="modal" data-target="#myModal">打开模态框</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="myModalLabel">模态框标题</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
模态框内容
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
以上是两种常见的解决方法,根据具体需求选择适合的方法来实现模态框功能。
领取专属 10元无门槛券
手把手带您无忧上云