hx-trigger

hx-trigger 属性允许你指定触发 AJAX 请求的内容。触发器值可以是以下值之一:

¥The hx-trigger attribute allows you to specify what triggers an AJAX request. A trigger value can be one of the following:

标准事件

¥Standard Events

标准事件引用 web API 事件(例如单击、键盘按下、鼠标松开、加载)。

¥Standard events refer to web API events (e.g. click, keydown, mouseup, load).

可以将标准事件(例如 click)指定为触发器,如下所示:

¥A standard event, such as click can be specified as the trigger like so:

<div hx-get="/clicked" hx-trigger="click">Click Me</div>

标准事件过滤器

¥Standard Event Filters

可以通过在事件名称后用方括号括起布尔 javascript 表达式来过滤事件。如果此表达式的计算结果为 true,则将触发该事件,否则将被忽略。标准事件过滤器 需要 eval

¥Events can be filtered by enclosing a boolean javascript expression in square brackets after the event name. If this expression evaluates to true the event will be triggered, otherwise it will be ignored. Standard event filters require eval.

<div hx-get="/clicked" hx-trigger="click[ctrlKey]">Control Click Me</div>

如果在 event.ctrlKey 属性设置为 true 的情况下触发单击事件,将触发此事件。

¥This event will trigger if a click event is triggered with the event.ctrlKey property set to true.

条件也可以引用全局函数或状态

¥Conditions can also refer to global functions or state

<div hx-get="/clicked" hx-trigger="click[checkGlobalState()]">Control Click Me</div>

也可以使用标准 javascript 语法进行组合

¥And can also be combined using the standard javascript syntax

<div hx-get="/clicked" hx-trigger="click[ctrlKey&&shiftKey]">Control-Shift Click Me</div>

请注意,表达式中使用的所有符号将首先针对触发事件进行解析,然后针对全局命名空间进行解析,因此 myEvent[foo] 将首先在事件上查找名为 foo 的属性,然后查找名为 foo 的全局符号

¥Note that all symbols used in the expression will be resolved first against the triggering event, and then next against the global namespace, so myEvent[foo] will first look for a property named foo on the event, then look for a global symbol with the name foo

标准事件修饰符

¥Standard Event Modifiers

标准事件也可以具有改变其行为方式的修饰符。修饰符是:

¥Standard events can also have modifiers that change how they behave. The modifiers are:

以下是搜索框的示例,该搜索框在 keyup 上进行搜索,但前提是搜索值已更改且用户 1 秒内未输入任何新内容:

¥Here is an example of a search box that searches on keyup, but only if the search value has changed and the user hasn’t typed anything new for 1 second:

<input name="q"
       hx-get="/search" hx-trigger="keyup changed delay:1s"
       hx-target="#search-results"/>

来自 /search URL 的响应将附加到 ID 为 search-resultsdiv

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

非标准事件

¥Non-standard Events

htmx 支持一些额外的非标准事件:

¥There are some additional non-standard events that htmx supports:

通过 HX-Trigger 标头触发

¥Triggering via the HX-Trigger header

如果你尝试从 HX-Trigger 响应标头触发事件,则可能需要使用 from:body 修饰符。例如如果你发送带有响应的类似 HX-Trigger: my-custom-event 的标头,则元素可能需要如下所示:

¥If you’re trying to fire an event from HX-Trigger response header, you will likely want to use the from:body modifier. E.g. if you send a header like this HX-Trigger: my-custom-event with a response, an element would likely need to look like this:

  <div hx-get="/example" hx-trigger="my-custom-event from:body">
    Triggered by HX-Trigger header...
  </div>

以便触发。

¥in order to fire.

这是因为标题可能会在与你希望触发的元素不同的 DOM 层次结构中触发事件。出于类似的原因,你经常会监听来自正文的热键。

¥This is because the header will likely trigger the event in a different DOM hierarchy than the element that you wish to be triggered. For a similar reason, you will often listen for hot keys from the body.

轮询

¥Polling

通过使用语法 every <timing declaration>,你可以定期轮询元素:

¥By using the syntax every <timing declaration> you can have an element poll periodically:

<div hx-get="/latest_updates" hx-trigger="every 1s">
  Nothing Yet!
</div>

此示例将每秒向 /latest_updates URL 发出 GET 并将结果交换到此 div 的 innerHTML 中。

¥This example will issue a GET to the /latest_updates URL every second and swap the results into the innerHTML of this div.

如果你希望向轮询添加过滤器,则应将其添加到轮询声明之后:

¥If you want to add a filter to polling, it should be added after the poll declaration:

<div hx-get="/latest_updates" hx-trigger="every 1s [someConditional]">
  Nothing Yet!
</div>

多个触发器

¥Multiple Triggers

可以提供多个触发器,用逗号分隔。每个触发器都有自己的选项。

¥Multiple triggers can be provided, separated by commas. Each trigger gets its own options.

  <div hx-get="/news" hx-trigger="load, click delay:1s"></div>

此示例将在页面加载时立即加载 /news,然后在每次单击后延迟一秒再次加载。

¥This example will load /news immediately on page load, and then again with a delay of one second after each click.

通过 JavaScript

¥Via JavaScript

AJAX 请求也可以通过 JavaScript htmx.trigger() 触发。

¥The AJAX request can be triggered via JavaScript htmx.trigger(), too.

注释

¥Notes