hx-swaphx-swap 属性允许您指定响应将如何相对于 AJAX 请求的 目标 进行替换。如果您没有指定选项,则默认值为 htmx.config.defaultSwapStyle (innerHTML)。
此属性的可能值包括:
innerHTML - 替换目标元素的内部 htmlouterHTML - 用响应替换整个目标元素textContent - 替换目标元素的文本内容,不将响应解析为 HTMLbeforebegin - 在目标元素之前插入响应afterbegin - 在目标元素的第一个子节点之前插入响应beforeend - 在目标元素的最后一个子节点之后插入响应afterend - 在目标元素之后插入响应delete - 无论响应如何,都删除目标元素none - 不追加响应中的内容(带外项目仍将被处理)。这些选项基于标准 DOM 命名和 Element.insertAdjacentHTML 规范。
因此,在此代码中
<div hx-get="/example" hx-swap="afterend">Get Some HTML & Append It</div>
div 将向 /example 发出请求,并在 div 之后追加返回的内容
hx-swap 属性支持修饰符以更改交换的行为。下面概述了这些修饰符。
transition如果您想在交换发生时使用新的 视图过渡 API,您可以使用您的交换的 transition:true 选项。您还可以通过将 htmx.config.globalViewTransitions 配置设置设置为 true 来全局启用此功能。
swap & settle您可以通过包含 swap 修饰符来修改 htmx 在收到响应后等待交换内容的时间。
<!-- this will wait 1s before doing the swap after it is received -->
<div hx-get="/example" hx-swap="innerHTML swap:1s">Get Some HTML & Append It</div>
同样,您可以通过包含 settle 修饰符来修改交换和结算逻辑之间的时间。
<!-- this will wait 1s before doing the swap after it is received -->
<div hx-get="/example" hx-swap="innerHTML settle:1s">Get Some HTML & Append It</div>
这些属性可用于将 htmx 与 CSS 过渡效果的时间同步。
ignoreTitle默认情况下,如果 htmx 在响应内容中找到 <title> 标签,它将更新页面的标题。您可以通过将 ignoreTitle 选项设置为 true 来关闭此行为。
scroll & show您还可以使用 scroll 和 show 修饰符来更改目标元素的滚动行为,这两个修饰符都接受 top 和 bottom 值。
<!-- this fixed-height div will scroll to the bottom of the div after content is appended -->
<div style="height:200px; overflow: scroll"
hx-get="/example"
hx-swap="beforeend scroll:bottom">
Get Some HTML & Append It & Scroll To Bottom
</div>
<!-- this will get some content and add it to #another-div, then ensure that the top of #another-div is visible in the
viewport -->
<div hx-get="/example"
hx-swap="innerHTML show:top"
hx-target="#another-div">
Get Some Content
</div>
如果您希望将滚动或显示目标设置为其他元素,您可以在 scroll: 或 show: 后面放置一个 CSS 选择器,后跟 :top 或 :bottom
<!-- this will get some content and swap it into the current div, then ensure that the top of #another-div is visible in the
viewport -->
<div hx-get="/example"
hx-swap="innerHTML show:#another-div:top">
Get Some Content
</div>
您还可以使用 window:top 和 window:bottom 将页面滚动到当前窗口的顶部和底部。
<!-- this will get some content and swap it into the current div, then ensure that the viewport is scrolled to the
very top -->
<div hx-get="/example"
hx-swap="innerHTML show:window:top">
Get Some Content
</div>
对于增强型链接和表单,默认行为是 show:top。您可以使用 htmx.config.scrollIntoViewOnBoost 全局禁用它,或者您可以在元素的基础上使用 hx-swap="show:none"。
<form action="/example" hx-swap="show:none">
...
</form>
htmx 会为具有定义的 id 属性的输入保留请求之间的焦点。默认情况下,htmx 会阻止请求之间自动滚动到聚焦的输入,这在用户已经向下滚动更长时间的请求中可能是不可取的行为。要启用焦点滚动,您可以使用 focus-scroll:true。
<input id="name" hx-get="/validation"
hx-swap="outerHTML focus-scroll:true"/>
或者,如果您希望页面在每次请求后自动滚动到聚焦的元素,您可以将 htmx 全局配置值 htmx.config.defaultFocusScroll 更改为 true。然后使用 focus-scroll:false 为特定请求禁用它。
<input id="name" hx-get="/validation"
hx-swap="outerHTML focus-scroll:false"/>
hx-swap 是继承的,可以放在父元素上innerHTML<body> 元素上使用 outerHTML 方法。htmx 将更改 <body> 上的 outerHTML 以使用 innerHTML。