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:

  1. true,在浏览器导航栏中替换获取的 URL。

    ¥true, which replaces the fetched URL in the browser navigation bar.

  2. false,如果由于继承而需要替换获取的 URL,则禁用替换该 URL。

    ¥false, which disables replacing the fetched URL if it would otherwise be replaced due to inheritance.

  3. 要替换到位置栏的 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