hx-target

hx-target 属性允许你将交换目标设为与发出 AJAX 请求的元素不同的元素。此属性的值可以是:

¥The hx-target attribute allows you to target a different element for swapping than the one issuing the AJAX request. The value of this attribute can be:

以下是针对 div 的示例:

¥Here is an example that targets a div:

<div>
    <div id="response-div"></div>
    <button hx-post="/register" hx-target="#response-div" hx-swap="beforeend">
        Register!
    </button>
</div>

来自 /register URL 的响应将附加到 ID 为 response-divdiv

¥The response from the /register url will be appended to the div with the id response-div.

此示例使用 hx-target="this" 创建一个在单击时自动更新的链接:

¥This example uses hx-target="this" to make a link that updates itself when clicked:

<a hx-post="/new-link" hx-target="this" hx-swap="outerHTML">New link</a>

注释

¥Notes