logo
🎨 前端开发

HTMX

HTMX Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 前端开发🧭 Markdown 速查🏷️ 1 个标签
#htmx
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

CDN Import
HTML
滚动查看更多
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
Basic Usage
HTML
滚动查看更多
<button hx-get="/hello" hx-target="#result">Say Hi</button>
<div id="result"></div>
Server Response
HTML
滚动查看更多
<!-- /hello response -->
<p>Hello from server</p>

Core Attributes

`hx-get`, `hx-post`, etc.
HTML
滚动查看更多
<a hx-get="/page">Load Page</a>
<form hx-post="/submit"></form>
`hx-target`
HTML
滚动查看更多
<button hx-get="/data" hx-target="#box"></button>
<div id="box"></div>
`hx-trigger`
HTML
滚动查看更多
<input hx-get="/search" hx-trigger="keyup changed delay:300ms" hx-target="#results" />

Swap & Out of Band

`hx-swap`
HTML
滚动查看更多
<div hx-get="/frag" hx-swap="innerHTML"></div>
  • outerHTML
  • innerHTML
  • beforebegin, afterbegin, etc.
`hx-swap-oob`
HTML
滚动查看更多
<div hx-swap-oob="true" id="msg"></div>

Useful for global updates from partials.

Swap Modifiers
HTML
滚动查看更多
hx-swap="outerHTML transition:true swap:1s"

Forms & Events

Auto POST on Submit
HTML
滚动查看更多
<form hx-post="/submit" hx-target="#status">
	<input name="name" />
	<button type="submit">Send</button>
</form>
<div id="status"></div>
`hx-include`
HTML
滚动查看更多
<input id="user-id" name="id" /> <button hx-post="/update" hx-include="#user-id">Update</button>
`hx-vals`
HTML
滚动查看更多
<button hx-post="/save" hx-vals='{"id": 42, "active": true}'>Save</button>

Advanced Features

Loading Indicator
HTML
滚动查看更多
<button hx-get="/load" hx-indicator="#spinner">Load</button>
<div id="spinner" class="htmx-indicator">Loading...</div>
`hx-push-url`
HTML
滚动查看更多
<a hx-get="/page" hx-push-url="true">Go</a>
Polling
HTML
滚动查看更多
<div hx-get="/time" hx-trigger="every 5s"></div>

Events & Extensions

Listen to Events
JS
滚动查看更多
document.body.addEventListener('htmx:afterSwap', e => {
	console.log('Swap complete');
});
Event Hooks
  • htmx:beforeRequest
  • htmx:afterSwap
  • htmx:responseError
Extensions
HTML
滚动查看更多
<script src="https://unpkg.com/htmx.org/dist/ext/json-enc.js"></script>
<form hx-post="/api" hx-ext="json-enc"></form>

Example Use Case

Python Backend (Flask)
PYTHON
滚动查看更多
@app.route("/hello")
def hello():
    return "<p>Hello, HTMX!</p>"
HTML Client
HTML
滚动查看更多
<button hx-get="/hello" hx-target="#msg">Click</button>
<div id="msg"></div>

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572