hx-replace-url
hx-replace-url
属性允许你替换浏览器 位置历史记录 的当前 URL。
¥The hx-replace-url
attribute allows you to replace the current url of the browser location history.
此属性的可能值为:
¥The possible values of this attribute are:
true
,在浏览器导航栏中替换获取的 URL。
¥true
, which replaces the fetched URL in the browser navigation bar.
false
,如果由于继承而需要替换获取的 URL,则禁用替换该 URL。
¥false
, which disables replacing the fetched URL if it would otherwise be replaced due to inheritance.
要替换到位置栏的 URL。根据 history.replaceState()
,这可能是相对的或绝对的。
¥A URL to be replaced into the location bar.
This may be relative or absolute, as per history.replaceState()
.
这是一个例子:
¥Here is an example:
<div hx-get="/account" hx-replace-url="true">
Go to My Account
</div>
这将导致 htmx 将当前 DOM 快照到 localStorage
并替换浏览器位置栏中的 URL“/account”。
¥This will cause htmx to snapshot the current DOM to localStorage
and replace the URL `/account’ in the browser location bar.
另一个例子:
¥Another example:
<div hx-get="/account" hx-replace-url="/account/home">
Go to My Account
</div>
这将替换浏览器位置栏中的 URL `/account/home’。
¥This will replace the URL `/account/home’ in the browser location bar.
¥Notes
hx-replace-url
是继承的,可以放在父元素上
¥hx-replace-url
is inherited and can be placed on a parent element
HX-Replace-Url
响应标头 具有类似的行为,可以覆盖此属性。
¥The HX-Replace-Url
response header has similar behavior and can override this attribute.
hx-history-elt
属性 允许更改保存在历史缓存中的元素。
¥The hx-history-elt
attribute allows changing which element is saved in the history cache.
hx-push-url
属性 是一个类似且更常用的属性,它创建一个新的历史记录条目,而不是替换当前的条目。
¥The hx-push-url
attribute is a similar and more commonly used attribute, which creates a
new history entry rather than replacing the current one.