hx-disinherit

htmx的默认行为是自动"继承"许多属性:例如,像hx-target这样的属性可以放在父元素上,所有子元素都会继承该目标。

hx-disinherit属性允许您控制这种自动属性继承。一个典型场景是允许您在页面的body元素上放置hx-boost,但在页面的特定部分覆盖该行为以实现更具体的功能。

htmx按以下方式评估属性继承:

<div hx-boost="true" hx-select="#content" hx-target="#content" hx-disinherit="*">
  <a href="/page1">转到页面1</a> <!-- 使用上述属性设置进行boost -->
  <a href="/page2" hx-boost="unset">转到页面1</a> <!-- 不使用boost -->
  <button hx-get="/test" hx-target="this"></button> <!-- hx-select不被继承 -->
</div>
<div hx-boost="true" hx-select="#content" hx-target="#content" hx-disinherit="hx-target">
  <!-- hx-select自动设置为父级的值;hx-target不被继承 -->
  <button hx-get="/test"></button>
</div>
<div hx-select="#content">
  <div hx-boost="true" hx-target="#content" hx-disinherit="hx-select">
    <!-- hx-target自动继承父级的值 -->
    <!-- hx-select不被继承,因为直接父级禁用了继承,
    尽管它本身没有指定hx-select -->
    <button hx-get="/test"></button>
  </div>
</div>

注意事项