# Htmx

**htmx** 是一个小巧、零依赖的 [JavaScript](https://www.edgepedia.org/zh/javascript) 库，它让网页能够发起 AJAX 请求、使用 [WebSocket](https://www.edgepedia.org/zh/websockets) 和服务器发送事件，并直接通过 HTML 属性而不是 JavaScript 代码来应用 CSS 过渡。一个带有 `hx-post="/clicked"` 和 `hx-target="#parent-div"` 的按钮，在被点击时会发出 HTTP POST，并把返回的 HTML 交换到具有该 id 的元素中。使用 htmx 的服务器通常返回 HTML 片段而不是 JSON，从而把应用逻辑留在服务器上，形成项目所称的“超媒体驱动应用”。<sup>[1](https://htmx.org/docs/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个面向浏览器的 JavaScript 库，可直接通过 HTML 属性访问现代浏览器特性；只需添加一个 `<script>` 标签即可安装，无需构建系统<sup>[1](https://htmx.org/docs/)</sup> |
| 体积 | 版本 2.0.10 的 `dist/htmx.min.js` 代码压缩后为 51,238 字节，再经 gzip 压缩后为 16,588 字节（约 16.2 KB），零依赖<sup>[2](https://solodevstack.com/blog/react-vs-htmx-solo-developers)</sup> |
| 许可证 | 0BSD（零条款 BSD），连署名要求也免除了<sup>[2](https://solodevstack.com/blog/react-vs-htmx-solo-developers)</sup> |
| 起源 | 最初是 intercooler.js，一个由卡森·格罗斯（Carson Gross）于 2013 年发布的基于 jQuery 的属性库；htmx 是它的独立重写版<sup>[3](https://se-radio.net/2025/06/se-radio-671-carson-gross-on-htmx/)</sup><sup> • </sup><sup>[4](https://intercoolerjs.org/)</sup> |
| 当前版本 | htmx 2.0.11 于 2026-09-22 发布；htmx 4.0.0 于 2026-08-28 发布，内部机制从 XMLHttpRequest 迁移到 `fetch()`<sup>[5](https://github.com/bigskysoftware/htmx/blob/master/CHANGELOG.md)</sup><sup> • </sup><sup>[6](https://four.htmx.org/announcements/2026-08-28-htmx-4.0.0-is-released)</sup> |
| 采用情况 | 2026-05-21 至 27 当周，GitHub 星标 48,117，npm 周下载量 158,476，而 React 为 245,311 星标和 129,315,465 次下载<sup>[2](https://solodevstack.com/blog/react-vs-htmx-solo-developers)</sup> |
| 性能 | 在 State of Web Vitals 2026 数据集中，htmx 通过最大内容绘制指标的比例在所有框架中最高，为 95.5%<sup>[7](https://www.corewebvitals.io/state-of-cwv/explorer/technologies/frameworks/by-lcp/desktop)</sup> |

## 工作原理

htmx 对 HTML 锚点标签（`a` 标签）的行为做了泛化。一个 `<a href="/blog">` 元素告诉浏览器发起 GET 请求，并把响应加载到窗口中；htmx 对此加以扩展，使任何元素都能以任意 HTTP 动词发起请求，任何事件都能触发请求，并且任何元素——而不只是整个窗口——都可以成为更新的目标。<sup>[1](https://htmx.org/docs/)</sup>

核心属性是 `hx-get`、`hx-post`、`hx-put`、`hx-patch` 和 `hx-delete`，各接收一个 URL。默认情况下，请求由元素自身的事件触发：输入元素为 `change`，表单为 `submit`，其余一律为 `click`。`hx-trigger` 属性可覆盖这一行为，并接受诸如 `changed`、`delay:<time>`（一种在新事件到来时重置的防抖）、`throttle:<time>`、`from:<CSS selector>`（用于监听另一个元素）以及 `every 2s`（用于轮询）等修饰符。<sup>[1](https://htmx.org/docs/)</sup>

**交换与目标定位。** 响应 HTML 依据 `hx-swap` 的值插入 DOM，其取值包括 `innerHTML`（默认值）、`outerHTML`、`afterbegin`、`beforeend`、`afterend`、`delete` 和 `none`。`hx-target` 可将交换指向另一个元素，并支持 `closest tr`、`next`、`previous`、`find` 等扩展选择器。通过 `hx-swap-oob` 进行的带外交换，可以让一个响应按 id 更新页面其他位置上的元素，于是服务器可以在一个看似无关的请求中捎带更新。<sup>[1](https://htmx.org/docs/)</sup><sup> • </sup><sup>[8](https://htmx.org/reference/)</sup>

一个边输入边搜索的输入框可以说明这种风格：`<input hx-get="/search" hx-trigger="keyup changed delay:500ms" hx-target="#results">` 会在输入值发生变化的按键释放事件发生 500 毫秒后发起请求，并把返回的片段插入结果 div，无需任何客户端防抖代码。<sup>[1](https://htmx.org/docs/)</sup>

htmx 还能不写 JavaScript 就实现 CSS 过渡：如果换入的内容与被替换的元素保持着相同的 `id`，htmx 会把旧属性复制到新元素上，将其换入，然后在默认 20 毫秒的稳定延迟之后应用新的属性值，从而让两个状态之间的 CSS 过渡得以上演。<sup>[1](https://htmx.org/docs/)</sup> 其他特性包括：`hx-boost` 用于对普通链接和表单进行渐进增强，`hx-sync` 用于协调或中止相互竞争的请求，支持通过 URL 推送的浏览历史，以及一套扩展系统。<sup>[1](https://htmx.org/docs/)</sup>

## 历史

htmx 最初是 intercooler.js——一个围绕 jQuery 构建、通过 HTML 属性添加行为的库，由格罗斯于 2013 年发布。格罗斯表示，属性驱动这一想法的部分灵感来自 Angular 1.0 用属性来驱动行为的做法，并且早期用户告诉他这种方法很 RESTful。<sup>[3](https://se-radio.net/2025/06/se-radio-671-carson-gross-on-htmx/)</sup> intercooler.js 的灵感源自 pjax，那是 GitHub 联合创始人克里斯·万斯特拉斯（Chris Wanstrath）更早的一个超媒体库。<sup>[9](https://htmx.org/essays/interviews/chris-wanstrath/)</sup> 项目网站现在把 htmx 1.x 称作“intercooler 2.0”，并推荐所有新项目使用。<sup>[4](https://intercoolerjs.org/)</sup>

2.x 版本去掉了 jQuery 时代的基础，并和 1.x 一样“永久”得到支持；项目已声明不会有 htmx 3.0，因此基于 fetch 的重写版本直接用了 4.0 这个版本号。<sup>[10](https://htmx.org/essays/the-fetchening/)</sup> htmx 4.0.0 在历经八个月的工作后于 2026-08-28 发布，将内部机制从 [XMLHttpRequest](https://www.edgepedia.org/zh/xmlhttprequest) 迁移到 `fetch()`，重做了扩展模型，以基于 `sessionStorage` 的 `hx-history-cache` 扩展取代了 localStorage 历史缓存，并新增了 `<hx-partial>` 服务器发送的交换指令（并已向后移植到 2.0.11）。<sup>[6](https://four.htmx.org/announcements/2026-08-28-htmx-4.0.0-is-released)</sup><sup> • </sup><sup>[5](https://github.com/bigskysoftware/htmx/blob/master/CHANGELOG.md)</sup>

## 超媒体方法

htmx 是[超媒体](https://www.edgepedia.org/zh/hypermedia)驱动应用（HDA）模型最广为人知的实现；在该模型中，HTML 是客户端与服务器之间主要的通信媒介，这一思路与 Turbo Hotwire、Datastar 等库相通。<sup>[11](https://arxiv.org/abs/2607.29338)</sup> 项目的文章从 [HATEOAS](https://www.edgepedia.org/zh/hateoas)（超媒体作为应用状态引擎）出发进行论证：超媒体响应把可用的后续操作编码为链接，因此一个透支的银行账户只需给出一个存款链接，而 JSON API 客户端则必须额外知道 `status: overdrawn` 字段的含义以及应该调用哪些 URL。<sup>[12](https://htmx.org/essays/hateoas/)</sup>

这与更广泛的“HTML First”主张相呼应。一篇 2026 年提交给《Web 工程杂志》（Journal of Web Engineering）的论文指出，在 htmx 项目的语境下，向 HTML 靠拢大幅缩减了代码库规模，同时通过概念上的简洁性提升了可维护性；文中还报告了一个将 Yle 网站按照 HTML First 原则改造的案例研究所体现的性能收益。<sup>[13](https://arxiv.org/abs/2602.17193)</sup>

## 采用情况与实测结果

htmx 仍是一个少数派选择。在一份由坦佩雷大学（Tampere University）一篇学位论文绘制成图表的 2025 年开发者调查中，3% 的受访者（316 人）表示在工作中使用 HTMX，而 React 为 67%。<sup>[14](https://www.theseus.fi/bitstream/handle/10024/913639/Dorato_Noriin.pdf?isAllowed=y&sequence=2)</sup>

被引用最多的迁移案例是 Contexte，一个法国的 SaaS 产品。大卫·吉约（David Guillot）在 DjangoCon 2022 上报告说，将一个已使用两年的 React 界面替换为 Django 模板加 htmx，对一个 21,500 行的代码库来说大约花了两个月，并带来：代码量减少 67%（从 21,500 行降至 7,200 行），JavaScript 依赖减少 96%（从 255 个降至 9 个），Web 构建速度提升 88%（从 40 秒降至 5 秒），首次加载的可交互时间加快 50% 到 60%（从 2 至 6 秒降至 1 至 2 秒），内存使用减少 46%（从 75 MB 降至 45 MB），且用户体验没有损失。Python 代码增长了 140%（从 500 行增至 1,200 行），前后端分工消失后，整个团队都变成了全栈。<sup>[15](https://htmx.org/essays/a-real-world-react-to-htmx-port/)</sup> htmx 项目自己的文章提醒说，Contexte 是一个以内容为中心、展示文字和图像的应用，极其适合超媒体，并非每个应用都能指望得到这样的数字。<sup>[15](https://htmx.org/essays/a-real-world-react-to-htmx-port/)</sup>

一项受控的学术对比分别用 React 和 htmx 构建了同一个待办事项应用，发现 htmx 版本以更小的代码库和更简单的架构实现了相似的功能，在 CPU 和网络节流下[最大内容绘制](https://www.edgepedia.org/zh/largest-contentful-paint)指标好 15%，在禁用缓存时好 36%，而 React 在灵活性和静态类型检查方面仍保有优势。<sup>[16](https://www.finna.fi/Record/trepo.10024_232190)</sup>

## 适用与批评

评论者们的看法趋于一致：这是一种分工，而非优劣定论。htmx 适合以展示和操作服务端数据为中心的应用，例如表单、表格、内容管理和内部业务工具；它能与任何渲染 HTML 的后端配合，包括 Django、Rails、Go 和 PHP，且无需构建流水线。而具有复杂客户端状态的应用——如富文本编辑器、拖放界面、实时协作或离线优先应用——并不适合服务端驱动的模型，仍属于客户端框架的领地。<sup>[17](https://techloghub.com/compare/htmx-vs-react)</sup><sup> • </sup><sup>[18](https://nicepick.dev/compare/htmx-vs-react)</sup> 混合方案很常见：一个由服务端渲染的 htmx 应用，再挂载一两个以 React 组件形式实现的复杂部件。<sup>[18](https://nicepick.dev/compare/htmx-vs-react)</sup>

反复出现的批评包括：与 React 相比，就业市场和人才池较小；缺少现成的组件生态；如果还需要移动应用或第三方 API，就必须另行构建一个 JSON API；以及担心把行为放进标记中，若响应未做清洗，会招致跨站脚本攻击。格罗斯在一篇名为《htmx 很烂》的自我批评文章中收集了这些论点，其中有些言之有理，有些则是玩笑，文章就放在项目自己的网站上。<sup>[19](https://htmx.org/essays/htmx-sucks/)</sup> 该项目还以其不寻常的公众形象而闻名：格罗斯宣布每个人都是 htmx 的 CEO，网站上还有各种梗图，以及一首关于“JavaScript 疲劳”的俳句。<sup>[20](https://htmx.org/essays/lore/)</sup>

## References

1. [htmx Documentation, htmx.org](https://htmx.org/docs/)
2. [React vs htmx for Solo Developers, SoloDevStack](https://solodevstack.com/blog/react-vs-htmx-solo-developers)
3. [SE Radio 671: Carson Gross on HTMX, Software Engineering Radio](https://se-radio.net/2025/06/se-radio-671-carson-gross-on-htmx/)
4. [intercooler.js, project site](https://intercoolerjs.org/)
5. [htmx CHANGELOG.md, GitHub](https://github.com/bigskysoftware/htmx/blob/master/CHANGELOG.md)
6. [htmx 4.0.0 has been released!, htmx.org](https://four.htmx.org/announcements/2026-08-28-htmx-4.0.0-is-released)
7. [The State of Web Vitals 2026, Frameworks performance by LCP](https://www.corewebvitals.io/state-of-cwv/explorer/technologies/frameworks/by-lcp/desktop)
8. [htmx Reference, htmx.org](https://htmx.org/reference/)
9. [An interview with Chris Wanstrath aka @defunkt, Creator of pjax, htmx.org](https://htmx.org/essays/interviews/chris-wanstrath/)
10. [The fetch()ening, htmx.org](https://htmx.org/essays/the-fetchening/)
11. [BRHC: Backend-driven Reactive Hypermedia Controls with a Statically Typed Kotlin DSL, arXiv](https://arxiv.org/abs/2607.29338)
12. [HATEOAS, htmx.org essays](https://htmx.org/essays/hateoas/)
13. [The Case for HTML First Web Development, arXiv](https://arxiv.org/abs/2602.17193)
14. [Dorato, Noriin, thesis with State of JS framework usage chart, Tampere University](https://www.theseus.fi/bitstream/handle/10024/913639/Dorato_Noriin.pdf?isAllowed=y&sequence=2)
15. [A Real World React -> htmx Port, htmx.org](https://htmx.org/essays/a-real-world-react-to-htmx-port/)
16. [Examining Htmx: A Tool Assisting in Building Interactive Multipage Full Stack Web Applications, Tampere University Trepo](https://www.finna.fi/Record/trepo.10024_232190)
17. [htmx vs React, TechLogHub](https://techloghub.com/compare/htmx-vs-react)
18. [HTMX vs React, Nice Pick](https://nicepick.dev/compare/htmx-vs-react)
19. [htmx sucks, htmx.org](https://htmx.org/essays/htmx-sucks/)
20. [htmx lore, htmx.org](https://htmx.org/essays/lore/)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/htmx

English original: https://www.edgepedia.org/htmx
