Htmx 提供了一個廣泛的事件系統,可用於修改和增強行為。事件列表如下。
htmx:abort
這個事件與其他事件不同:htmx 並不觸發它,而是監聽它。
如果您向正在發出請求的元素發送 htmx:abort
事件,它將中止該請求
<button id="request-button" hx-post="/example">Issue Request</button>
<button onclick="htmx.trigger('#request-button', 'htmx:abort')">Cancel Request</button>
htmx:afterOnLoad
此事件在 AJAX onload
完成後觸發。請注意,這並不意味著內容已交換或完成,僅表示請求已完成。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:afterProcessNode
此事件在 htmx 初始化 DOM 節點後觸發。對於擴展在節點上建立額外功能很有用。
detail.elt
- 發出請求的元素htmx:afterRequest
此事件在 AJAX 請求完成後觸發,無論是請求成功的情況(即使是返回了遠端錯誤代碼(例如 404
)),還是在網路錯誤的情況下。此事件可以與 htmx:beforeRequest
配對,以將行為包裝在請求週期周圍。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置detail.successful
- 如果回應具有 20x 狀態碼,或在 htmx:beforeSwap
事件中標記為 detail.isError = false
,則為 true,否則為 falsedetail.failed
- 如果回應沒有 20x 狀態碼,或在 htmx:beforeSwap
事件中標記為 detail.isError = true
,則為 true,否則為 falsehtmx:afterSettle
此事件在 DOM 完成後觸發。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:afterSwap
此事件在新內容交換到 DOM 中後觸發。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:beforeCleanupElement
此事件在 htmx 停用元素或將其從 DOM 中移除之前觸發。
detail.elt
- 清理的元素htmx:beforeOnLoad
此事件在任何回應處理發生之前觸發。如果事件被取消,則不會發生交換。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:beforeProcessNode
此事件在 htmx 初始化 DOM 節點並處理完其所有 hx-
屬性之前觸發。這使擴展和其他外部程式碼能夠在處理 DOM 節點之前修改其內容。
detail.elt
- 發出請求的元素htmx:beforeRequest
此事件在發出 AJAX 請求之前觸發。如果事件被取消,則不會發生請求。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:beforeSend
此事件在請求傳送之前立即觸發。您無法使用此事件取消請求。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:beforeSwap
此事件在任何新內容交換到 DOM 中之前觸發。如果事件被取消,則不會發生交換。
您可以通過修改事件詳細資訊的 shouldSwap
和 target
屬性來修改預設交換行為。有關更多詳細資訊,請參閱關於配置交換的文件。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.requestConfig
- AJAX 請求的配置detail.shouldSwap
- 如果將交換內容(對於非 200 回應代碼,預設為 false
)detail.ignoreTitle
- 如果為 true
,則會忽略回應中的任何 title 標籤detail.target
- 交換的目標htmx:beforeTransition
此事件在發生檢視轉換包裝的交換之前觸發。如果事件被取消,則不會發生檢視轉換,而是會發生正常的交換邏輯。
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.requestConfig
- AJAX 請求的配置detail.shouldSwap
- 如果將交換內容(對於非 200 回應代碼,預設為 false
)detail.target
- 交換的目標htmx:configRequest
此事件在 htmx 收集完要包含在請求中的參數後觸發。它可以用於包含或更新 htmx 將傳送的參數
document.body.addEventListener('htmx:configRequest', function(evt) {
evt.detail.parameters['auth_token'] = getAuthToken(); // add a new parameter into the mix
});
請注意,如果一個輸入值出現多次,則 parameters
物件中的值將是一個陣列,而不是單個值。
detail.parameters
- 將在請求中提交的參數detail.unfilteredParameters
- 在按 hx-select
篩選之前找到的參數detail.headers
- 請求標頭detail.elt
- 觸發請求的元素detail.target
- 請求的目標detail.verb
- 使用的 HTTP 動詞htmx:confirm
此事件在每次觸發請求時觸發(不只是在具有 hx-confirm 屬性的元素上)。它允許您取消(或延遲)發出 AJAX 請求。如果您在事件上呼叫 preventDefault()
,則不會發出給定的請求。detail
物件包含一個函數 evt.detail.issueRequest(skipConfirmation=false)
,可用於在稍後發出實際的 AJAX 請求。將這兩個功能結合使用,您可以建立一個非同步確認對話方塊。
以下是一個基本範例,顯示 htmx:confirm
事件的基本用法,而不更改預設行為
document.body.addEventListener('htmx:confirm', function(evt) {
// 0. To modify the behavior only for elements with the hx-confirm attribute,
// check if evt.detail.target.hasAttribute('hx-confirm')
// 1. Prevent the default behavior (this will prevent the request from being issued)
evt.preventDefault();
// 2. Do your own logic here
console.log(evt.detail)
// 3. Manually issue the request when you are ready
evt.detail.issueRequest(); // or evt.detail.issueRequest(true) to skip the built-in window.confirm()
});
以下是一個範例,在具有 confirm-with-sweet-alert="{question}"
屬性的任何元素上使用 sweet alert
document.body.addEventListener('htmx:confirm', function(evt) {
// 1. The requirement to show the sweet alert is that the element has a confirm-with-sweet-alert
// attribute on it, if it doesn't we can return early and let the default behavior happen
if (!evt.detail.target.hasAttribute('confirm-with-sweet-alert')) return
// 2. Get the question from the attribute
const question = evt.detail.target.getAttribute('confirm-with-sweet-alert');
// 3. Prevent the default behavior (this will prevent the request from being issued)
evt.preventDefault();
// 4. Show the sweet alert
swal({
title: "Are you sure?",
text: question || "Are you sure you want to continue?",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((confirmed) => {
if (confirmed) {
// 5. If the user confirms, we can manually issue the request
evt.detail.issueRequest(true); // true to skip the built-in window.confirm()
}
});
});
detail.elt
- 有問題的元素detail.etc
- 其他請求資訊(大部分未使用)detail.issueRequest(skipConfirmation=false)
- 可用於發出請求的函數(應與 evt.preventDefault()
配對!),如果 skipConfirmation 為 true
,則不會執行原始的 window.confirm()
detail.path
- 請求的路徑detail.target
- 觸發請求的元素detail.triggeringEvent
- 觸發此請求的原始事件detail.verb
- 請求的動詞(例如 GET
)detail.question
- 傳遞給 hx-confirm
屬性的問題(僅在存在 hx-confirm
屬性時可用)htmx:historyCacheError
當嘗試將快取儲存到 localStorage
失敗時,會觸發此事件
detail.cause
- 嘗試將歷史記錄儲存到 localStorage
時擲出的 Exception
htmx:historyCacheMiss
當還原歷史記錄時發生快取遺失時,會觸發此事件
detail.xhr
- 將擷取遠端內容以進行還原的 XMLHttpRequest
detail.path
- 正在還原的頁面的路徑和查詢htmx:historyCacheMissError
當發生快取遺失並且已從伺服器擷取要還原的內容的回應,但該回應是錯誤(例如 404
)時,會觸發此事件
detail.xhr
- XMLHttpRequest
detail.path
- 正在還原的頁面的路徑和查詢htmx:historyCacheMissLoad
當發生快取遺失,並且已從伺服器成功擷取要還原的內容的回應時,會觸發此事件
detail.xhr
- XMLHttpRequest
detail.path
- 正在還原的頁面的路徑和查詢htmx:historyRestore
當 htmx 處理歷史記錄還原操作時,會觸發此事件
detail.path
- 正在還原的頁面的路徑和查詢htmx:beforeHistorySave
此事件在內容儲存到歷史記錄 API 之前觸發。
detail.path
- 正在還原的頁面的路徑和查詢detail.historyElt
- 要還原到的歷史記錄元素detail.config
- 將傳遞給 EventSource
建構函式的配置htmx:load
當 htmx 將新節點載入到 DOM 中時,會觸發此事件。
detail.elt
- 新增的元素htmx:noSSESourceError
當元素在其觸發器中引用 SSE 事件,但未定義父級 SSE 來源時,會觸發此事件
detail.elt
- 具有錯誤 SSE 觸發器的元素htmx:oobAfterSwap
detail.elt
- 發出請求的元素detail.shouldSwap
- 如果將交換內容(預設為 true
)detail.target
- 交換的目標detail.fragment
- 回應片段htmx:oobBeforeSwap
detail.elt
- 發出請求的元素detail.shouldSwap
- 如果將交換內容(預設為 true
)detail.target
- 交換的目標detail.fragment
- 回應片段htmx:oobErrorNoTarget
當頻外交換在 DOM 中找不到對應的元素來進行交換時,會觸發此事件。
detail.content
- 具有錯誤頻外 id
的元素htmx:onLoadError
當 AJAX 呼叫的 load
處理過程中發生錯誤時,會觸發此事件
detail.xhr
- XMLHttpRequest
detail.elt
- 觸發請求的元素detail.target
- 請求的目標detail.exception
- 發生的例外狀況detail.requestConfig
- AJAX 請求的配置htmx:prompt
在使用hx-prompt
屬性向使用者顯示提示後,會觸發此事件。如果此事件被取消,則不會發生 AJAX 請求。
detail.elt
- 觸發請求的元素detail.target
- 請求的目標detail.prompt
- 使用者對提示的回應htmx:beforeHistoryUpdate
此事件會在執行歷史記錄更新之前觸發。它可用於修改用於更新歷史記錄的 path
或 type
。
detail.history
- 歷史記錄更新的 path
和 type
(push, replace)detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:pushedIntoHistory
此事件會在 URL 被推入歷史記錄後觸發。
detail.path
- 已推入歷史記錄的 URL 的路徑和查詢htmx:replacedInHistory
此事件會在 URL 被替換到歷史記錄後觸發。
detail.path
- 已替換到歷史記錄的 URL 的路徑和查詢htmx:responseError
當發生 HTTP 錯誤回應時,會觸發此事件
detail.xhr
- XMLHttpRequest
detail.elt
- 觸發請求的元素detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:sendError
當網路錯誤阻止 HTTP 請求發生時,會觸發此事件
detail.xhr
- XMLHttpRequest
detail.elt
- 觸發請求的元素detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:sseError
當 SSE 來源發生錯誤時,會觸發此事件
detail.elt
- 具有錯誤 SSE 來源的元素detail.error
- 錯誤detail.source
- SSE 來源htmx:swapError
當交換階段發生錯誤時,會觸發此事件
detail.xhr
- XMLHttpRequest
detail.elt
- 觸發請求的元素detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:targetError
當為 hx-target
屬性使用錯誤的選擇器時(例如,沒有前導 #
的元素 ID),會觸發此事件
detail.elt
- 觸發請求的元素detail.target
- 錯誤的 CSS 選擇器htmx:timeout
當請求逾時發生時,會觸發此事件。它封裝了 XMLHttpRequest 的典型 timeout
事件。
可以使用 htmx.config.timeout
或每個元素使用 hx-request
來設定逾時時間
detail.elt
- 發出請求的元素detail.xhr
- XMLHttpRequest
detail.target
- 請求的目標detail.requestConfig
- AJAX 請求的配置htmx:trigger
每當需要發出 AJAX 請求時,即使沒有指定 AJAX 請求,也會觸發此事件。它主要用於允許 hx-trigger
執行客戶端腳本;AJAX 請求有更多可用的細化事件,例如 htmx:beforeRequest
或 htmx:afterRequest
。
detail.elt
- 觸發請求的元素htmx:validateUrl
此事件會在發出請求之前觸發,讓您可以驗證 htmx 將要請求的 URL。如果在事件上調用 preventDefault()
,則不會發出請求。
document.body.addEventListener('htmx:validateUrl', function (evt) {
// only allow requests to the current server as well as myserver.com
if (!evt.detail.sameHost && evt.detail.url.hostname !== "myserver.com") {
evt.preventDefault();
}
});
detail.elt
- 觸發請求的元素detail.url
- 表示將發送請求的 URL 的 URL 物件。detail.sameHost
- 如果請求與文件位於同一主機,則為 true
htmx:validation:validate
此事件會在驗證元素之前觸發。它可以使用 elt.setCustomValidity()
方法來實現自訂驗證規則。
<form hx-post="/test">
<input _="on htmx:validation:validate
if my.value != 'foo'
call me.setCustomValidity('Please enter the value foo')
else
call me.setCustomValidity('')"
name="example">
</form>
detail.elt
- 觸發請求的元素htmx:validation:failed
當元素驗證失敗時,會觸發此事件。
detail.elt
- 觸發請求的元素detail.message
- 驗證錯誤訊息detail.validity
- 有效性物件,其中包含指定驗證失敗方式的屬性htmx:validation:halted
當由於驗證錯誤而暫停請求時,會觸發此事件。
detail.elt
- 觸發請求的元素detail.errors
- 包含無效元素和與其相關聯的錯誤的錯誤物件陣列htmx:xhr:abort
當 ajax 請求中止時,會觸發此事件
detail.elt
- 觸發請求的元素htmx:xhr:loadstart
當 ajax 請求開始時,會觸發此事件
detail.elt
- 觸發請求的元素htmx:xhr:loadend
當 ajax 請求完成時,會觸發此事件
detail.elt
- 觸發請求的元素htmx:xhr:progress
當支援進度的 ajax 請求正在進行中時,會定期觸發此事件
detail.elt
- 觸發請求的元素