许多 CSS 工具包都包含用于创建模态对话框的样式(和 Javascript)。本示例展示了如何将 HTMX 与 Bootstrap 提供的原始 JavaScript 一起使用。
我们从一个触发对话框的按钮开始,以及一个在你的标记底部用于加载对话框的 DIV。
<button
hx-get="/modal"
hx-target="#modals-here"
hx-trigger="click"
data-bs-toggle="modal"
data-bs-target="#modals-here"
class="btn primary">Open Modal</button>
<div id="modals-here"
class="modal modal-blur fade"
style="display: none"
aria-hidden="false"
tabindex="-1">
<div class="modal-dialog modal-lg modal-dialog-centered" role="document">
<div class="modal-content"></div>
</div>
</div>
此按钮在单击时使用 GET
请求发送到 /modal
。此文件的内容将添加到 #modals-here
DIV 下面的 DOM 中。
服务器使用 Bootstrap 标准模态的略微修改版本进行响应。
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>