htmx

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

HTMX Skill

HTMX 技能指南

Use this skill for HTMX implementation and integration. Read only the reference file(s) needed for the task.
本指南适用于HTMX的实现与集成。仅阅读任务所需的参考文件即可。

Quick Start

快速入门

  1. Identify the domain of the task (attributes, requests, swapping, events, patterns).
  2. Open the matching file from
    references/
    .
  3. Implement using HTML-first, hypermedia-driven patterns.
  4. Validate that server responses return HTML fragments, not JSON.
  1. 确定任务所属领域(属性、请求、替换、事件、模式)。
  2. references/
    目录中打开对应的文件。
  3. 采用HTML优先、超媒体驱动的模式进行实现。
  4. 验证服务器响应返回的是HTML片段而非JSON。

Critical Rules

核心规则

  • HTMX expects HTML responses from the server, not JSON.
  • Most attributes inherit to child elements. Not inherited:
    hx-trigger
    ,
    hx-on*
    ,
    hx-swap-oob
    ,
    hx-preserve
    ,
    hx-history-elt
    ,
    hx-validate
    . Use
    hx-disinherit
    or
    unset
    to stop inheritance of other attributes.
  • Default swap strategy is
    innerHTML
    . Always confirm the intended swap method.
  • Non-GET requests automatically include the closest enclosing form's values.
  • Use
    hx-boost="true"
    for progressive enhancement — pages must work without JS.
  • Escape all user-supplied content server-side to prevent XSS.
  • HTMX adds/removes CSS classes during the request lifecycle — use these for transitions and indicators.
  • All
    hx-*
    attributes can also be written as
    data-hx-*
    for HTML validation compliance.
  • HTMX期望服务器返回HTML响应,而非JSON。
  • 大多数属性会继承到子元素。不继承的属性包括
    hx-trigger
    hx-on*
    hx-swap-oob
    hx-preserve
    hx-history-elt
    hx-validate
    。可使用
    hx-disinherit
    unset
    来阻止其他属性的继承。
  • 默认的替换策略是
    innerHTML
    。请务必确认预期的替换方式。
  • 非GET请求会自动包含最近的父表单中的值。
  • 使用
    hx-boost="true"
    实现渐进式增强——页面在无JavaScript的情况下也必须能正常工作。
  • 在服务器端转义所有用户提供的内容以防止XSS攻击。
  • HTMX会在请求生命周期中添加/移除CSS类——可利用这些类实现过渡效果和状态提示。
  • 所有
    hx-*
    属性也可以写成
    data-hx-*
    格式,以符合HTML验证规范。

Reference Map

参考文档映射

  • All
    hx-*
    attributes, values, and modifiers:
    references/attributes.md
  • Triggers, headers, parameters, CSRF, caching, CORS:
    references/requests.md
  • Swap methods, targets, OOB swaps, morphing, view transitions:
    references/swapping.md
  • Events, JS API, configuration, extensions, debugging:
    references/events-api.md
  • Common UI patterns and examples:
    references/patterns.md
  • Cross-file index and routing:
    references/REFERENCE.md
  • 所有
    hx-*
    属性、值及修饰符:
    references/attributes.md
  • 触发器、请求头、参数、CSRF、缓存、CORS:
    references/requests.md
  • 替换方法、目标元素、OOB替换、形态变化、视图过渡:
    references/swapping.md
  • 事件、JS API、配置、扩展、调试:
    references/events-api.md
  • 常见UI模式及示例:
    references/patterns.md
  • 跨文件索引与路由:
    references/REFERENCE.md

Task Routing

任务路由

  • Adding HTMX behavior to elements ->
    references/attributes.md
  • Configuring how/when requests fire ->
    references/requests.md
  • Controlling where/how responses render ->
    references/swapping.md
  • Handling events, JS interop, or config ->
    references/events-api.md
  • Building common UI patterns (search, infinite scroll, modals, etc.) ->
    references/patterns.md
  • Cross-cutting concerns or architecture ->
    references/REFERENCE.md
    then domain-specific files
  • 为元素添加HTMX行为 ->
    references/attributes.md
  • 配置请求的触发方式/时机 ->
    references/requests.md
  • 控制响应的渲染位置/方式 ->
    references/swapping.md
  • 处理事件、JS交互或配置 ->
    references/events-api.md
  • 构建常见UI模式(搜索、无限滚动、模态框等) ->
    references/patterns.md
  • 跨领域问题或架构设计 -> 先查看
    references/REFERENCE.md
    ,再查阅领域特定文件