许多 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>