此扩展允许你指定在收到不同的 HTTP 响应代码时要交换的不同目标元素。
¥This extension allows you to specify different target elements to be swapped when different HTTP response codes are received.
它使用 hx-target-[CODE]
形式的属性名称,其中 [CODE]
是数字 HTTP 响应代码,末尾带有可选通配符。你还可以使用 hx-target-error
,它处理 4xx 和 5xx 响应代码。
¥It uses attribute names in a form of hx-target-[CODE]
where [CODE]
is a numeric
HTTP response code with the optional wildcard character at its end. You can also use
hx-target-error
, which handles both 4xx and 5xx response codes.
每个属性的值可以是:
¥The value of each attribute can be:
要定位的元素的 CSS 查询选择器。
¥A CSS query selector of the element to target.
this
表示 hx-target
属性所在的元素是目标。
¥this
which indicates that the element that the hx-target
attribute is on is the target.
closest <CSS selector>
将查找与给定 CSS 选择器匹配的最近的父祖级(例如,closest tr
将定位元素最近的表格行)。
¥closest <CSS selector>
which will find the closest parent ancestor that matches the given CSS selector
(e.g. closest tr
will target the closest table row to the element).
find <CSS selector>
将查找与给定 CSS 选择器匹配的第一个子后代元素。
¥find <CSS selector>
which will find the first child descendant element that matches the given CSS selector.
next <CSS selector>
将向前扫描 DOM 以查找与给定 CSS 选择器匹配的第一个元素。(例如 next .error
将定位到具有 error
类的最近的后续兄弟元素)
¥next <CSS selector>
which will scan the DOM forward for the first element that matches the given CSS selector.
(e.g. next .error
will target the closest following sibling element with error
class)
previous <CSS selector>
将向后扫描 DOM 以查找与给定 CSS 选择器匹配的第一个元素。(例如 previous .error
将定位具有 error
类的最近前兄弟元素)
¥previous <CSS selector>
which will scan the DOM backwards for the first element that matches the given CSS selector.
(e.g previous .error
will target the closest previous sibling with error
class)
¥Installing
安装 response-targets
的最快方法是通过 CDN 加载它。请记住始终在扩展和 启用扩展 之前包含核心 htmx 库。
¥The fastest way to install response-targets
is to load it via a CDN. Remember to always include the core htmx library before the extension and enable the extension.
<head>
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
<script src="https://unpkg.com/htmx-ext-response-targets@2.0.2" integrity="sha384-T41oglUPvXLGBVyRdZsVRxNWnOOqCynaPubjUVjxhsjFTKrFJGEMm3/0KGmNQ+Pg" crossorigin="anonymous"></script>
</head>
<body hx-ext="response-targets">
...
https://unpkg.com/htmx-ext-response-targets/dist/response-targets.js 上还有一个未缩小的版本。
¥An unminified version is also available at https://unpkg.com/htmx-ext-response-targets/dist/response-targets.js.
虽然 CDN 方法很简单,但你可能需要考虑 在生产中不使用 CDN。安装 response-targets
的下一个最简单的方法是将其复制到你的项目中。从 https://unpkg.com/htmx-ext-response-targets
下载扩展,将其添加到项目中的相应目录中,并在必要时使用 <script>
标签将其包含在内。
¥While the CDN approach is simple, you may want to consider not using CDNs in production. The next easiest way to install response-targets
is to simply copy it into your project. Download the extension from https://unpkg.com/htmx-ext-response-targets
, add it to the appropriate directory in your project and include it where necessary with a <script>
tag.
对于 npm 样式的构建系统,你可以通过 npm 安装 response-targets
:
¥For npm-style build systems, you can install response-targets
via npm:
npm install htmx-ext-response-targets
安装后,你需要使用适当的工具来打包 node_modules/htmx-ext-response-targets/dist/response-targets.js
(或 .min.js
)。例如,你可以将扩展与来自 node_modules/htmx.org/dist/htmx.js
的 htmx 核心和项目特定代码打包在一起。
¥After installing, you’ll need to use appropriate tooling to bundle node_modules/htmx-ext-response-targets/dist/response-targets.js
(or .min.js
). For example, you might bundle the extension with htmx core from node_modules/htmx.org/dist/htmx.js
and project-specific code.
如果你使用打包器来管理你的 javascript(例如 Webpack、Rollup):
¥If you are using a bundler to manage your javascript (e.g. Webpack, Rollup):
通过 npm 安装 htmx.org
和 htmx-ext-response-targets
¥Install htmx.org
and htmx-ext-response-targets
via npm
将两个包导入到你的 index.js
¥Import both packages to your index.js
import `htmx.org`;
import `htmx-ext-response-targets`;
¥Configure (optional)
当收到 HX-Retarget
响应标头时,它会禁用此扩展将执行的任何查找,但任何带有错误状态代码的响应都将被交换(通常不会交换,即使通过标头设置目标也是如此)并且内部错误标志(isError
)将被修改。你可以通过将配置标志 htmx.config.responseTargetPrefersRetargetHeader
设置为 false
(默认为 true
)来更改此设置并选择在 hx-target-…
到位时忽略 HX-Retarget
标头。请注意,此扩展仅执行简单检查,以确定标头是否已设置且目标是否存在。它不是从标头中提取目标的值,而是相信它是由 HTMX 核心逻辑设置的。
¥When HX-Retarget
response header is received it disables any lookup that would be
performed by this extension but any responses with error status codes will be
swapped (normally they would not be, even with target set via header) and internal
error flag (isError
) will be modified. You may change this and choose to ignore
HX-Retarget
header when hx-target-…
is in place by setting a configuration flag
htmx.config.responseTargetPrefersRetargetHeader
to false
(default is
true
). Note that this extension only performs a simple check whether the header
is set and target exists. It is not extracting target’s value from the header but
trusts it was set by HTMX core logic.
通常,如果找到匹配的 hx-target-…
标签,则任何已由 HTMX 内置函数或之前调用的扩展建立的目标都将被覆盖。你可以通过使用配置标志 htmx.config.responseTargetPrefersExisting
到 true
(默认为 false
)来更改它。这是一个古怪且有风险的选择。它有一个现实生活中的应用,类似于一个熟练的、全栈的缓步动物在没有人看的时候吃括号。
¥Normally, any target which is already established by HTMX built-in functions or
extensions called before will be overwritten if a matching hx-target-…
tag is
found. You may change it by using a configuration flag
htmx.config.responseTargetPrefersExisting
to true
(default is false
). This is
kinky and risky option. It has a real-life applications similar to a skilled,
full-stack tardigrade eating parentheses when no one is watching.
当收到错误响应代码时,与将内容与 hx-target-[CODE]
交换相关联的事件的 detail
成员上的 isError
标志将设置为 false
。这与默认行为不同。你可以通过将配置标志 htmx.config.responseTargetUnsetsError
设置为 false
(默认为 true
)来更改此设置。
¥isError
flag on the detail
member of an event associated with swapping the
content with hx-target-[CODE]
will be set to false
when error response code is
received. This is different from the default behavior. You may change this by
setting a configuration flag htmx.config.responseTargetUnsetsError
to false
(default is true
).
当收到非错误响应代码时,与将内容与 hx-target-[CODE]
交换相关联的事件的 detail
成员上的 isError
标志将设置为 false
。这与默认行为没有什么不同。你可以通过将配置标志 htmx.config.responseTargetSetsError
设置为 true
(默认为 false
)来更改此设置。此设置不会影响响应代码 200,因为它不由此扩展处理。
¥isError
flag on the detail
member of an event associated with swapping the
content with hx-target-[CODE]
will be set to false
when non-erroneous response
code is received. This is no different from the default behavior. You may change
this by setting a configuration flag htmx.config.responseTargetSetsError
to
true
(default is false
). This setting will not affect the response code 200
since it is not handled by this extension.
¥Usage
以下是针对正常 (200) 响应的 div
示例,但针对 404(未找到)响应的另一个 div
示例,以及针对所有 5xx 响应代码的另一个 div
示例:
¥Here is an example that targets a div
for normal (200) response but another div
for 404 (not found) response, and yet another for all 5xx response codes:
<div hx-ext="response-targets">
<div id="response-div"></div>
<button hx-post="/register"
hx-target="#response-div"
hx-target-5*="#serious-errors"
hx-target-404="#not-found">
Register!
</button>
<div id="serious-errors"></div>
<div id="not-found"></div>
</div>
当响应代码为 200(OK)时,来自 /register
URL 的响应将用 id
response-div
替换 div
的内容。
¥The response from the /register
URL will replace contents of the div
with the
id
response-div
when response code is 200 (OK).
当响应代码以数字 5 开头(服务器错误)时,来自 /register
URL 的响应将用 id
serious-errors
替换 div
的内容。
¥The response from the /register
URL will replace contents of the div
with the id
serious-errors
when response code begins with a digit 5 (server errors).
当响应代码为 404(未找到)时,来自 /register
URL 的响应将用 id
not-found
替换 div
的内容。
¥The response from the /register
URL will replace contents of the div
with
the id
not-found
when response code is 404 (Not Found).
有时你可能不想分别处理 5xx 和 4xx 错误,在这种情况下你可以使用 hx-target-error
:
¥Sometimes you may not want to handle 5xx and 4xx errors separately, in which case you
can use hx-target-error
:
<div hx-ext="response-targets">
<div id="response-div"></div>
<button hx-post="/register"
hx-target="#response-div"
hx-target-error="#any-errors">
Register!
</button>
<div id="any-errors"></div>
</div>
2xx 代码将按照上例处理。但是,当响应代码为 5xx 或 4xx 时,来自 /register
的响应将用 id
any-errors
替换 div
的内容。
¥2xx codes will be handled as in the previous example. However, when the response code is 5xx
or 4xx, the response from /register
will replace the contents of the div
with the id
any-errors
.
¥Wildcard resolution
当状态响应代码与现有的 hx-target-[CODE]
属性名称不匹配时,其以字符串表示的数字部分将被修剪,最后一个字符将被替换为星号 (*
)。此查找过程持续到找到属性或没有更多数字为止。
¥When status response code does not match existing hx-target-[CODE]
attribute name
then its numeric part expressed as a string is trimmed with last character being
replaced with the asterisk (*
). This lookup process continues until the attribute
is found or there are no more digits.
例如,如果浏览器收到 404 错误代码,则会查找以下属性名称(按给定顺序):
¥For example, if a browser receives 404 error code, the following attribute names will be looked up (in the given order):
hx-target-404
hx-target-40*
hx-target-4*
hx-target-*
。
如果你使用的工具不支持 HTML 属性中的星号,你可以改用 x
字符,例如 hx-target-4xx
。
¥If you are using tools that do not support asterisks in HTML attributes, you
may instead use the x
character, e.g., hx-target-4xx
.
¥Notes
hx-target-…
是继承的,可以放在父元素上。
¥hx-target-…
is inherited and can be placed on a parent element.
hx-target-…
不能用于处理 HTTP 响应代码 200。
¥hx-target-…
cannot be used to handle HTTP response code 200.
hx-target-…
将默认遵守 HX-Retarget
,并且优先于任何计算目标,但可以通过禁用 htmx.config.responseTargetPrefersRetargetHeader
配置选项来更改。
¥hx-target-…
will honor HX-Retarget
by default and will prefer it over any
calculated target but it can be changed by disabling the
htmx.config.responseTargetPrefersRetargetHeader
configuration option.
为了避免意外,用于启用此扩展的 hx-ext
属性应放置在包含具有 hx-target-…
和 hx-target
属性的元素的父元素上。
¥To avoid surprises the hx-ext
attribute used to enable this extension should be
placed on a parent element containing elements with hx-target-…
and hx-target
attributes.
¥See also