hx-inherit

htmx 的默认行为是自动 “inherit” 许多属性:也就是说,可以将诸如 hx-target 之类的属性放在父元素上,并且所有子元素都将继承该目标。有些人不喜欢此功能,而是更喜欢明确指定属性的继承。

¥The default behavior for htmx is to “inherit” many attributes automatically: that is, an attribute such as hx-target may be placed on a parent element, and all child elements will inherit that target. Some people do not like this feature and instead prefer to explicitly specify inheritance for attributes.

为了支持这种开发模式,htmx 提供了 htmx.config.disableInheritance 设置,可以将其设置为 true 以防止继承成为任何 htmx 属性的默认行为。

¥To support this mode of development, htmx offers the htmx.config.disableInheritance setting, which can be set to true to prevent inheritance from being the default behavior for any of the htmx attributes.

hx-inherit 属性允许你手动控制属性的继承。

¥The hx-inherit attribute allows you to control the inheritance of attributes manually.

htmx 评估属性继承的方式如下:

¥htmx evaluates attribute inheritance as follows:

以下是当 htmx.config.disableInheritance 设置为 false 时,div 共享一组锚标签的 hx-target 属性的示例:

¥Here is an example of a div that shares an hx-target attribute for a set of anchor tags when htmx.config.disableInheritance is set to false:

<div hx-target="#tab-container" hx-inherit="hx-target">
  <a hx-boost="true" href="/tab1">Tab 1</a>
  <a hx-boost="true" href="/tab2">Tab 2</a>
  <a hx-boost="true" href="/tab3">Tab 3</a>
</div>

注释

¥Notes