許多 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>
當點擊此按鈕時,此按鈕會向 /modal
發出 GET
請求。此檔案的內容將會被新增到 #modals-here
DIV 的下方。
伺服器會以稍微修改過的 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>