{"id":322,"date":"2026-11-20T12:51:40","date_gmt":"2026-11-20T04:51:40","guid":{"rendered":"https:\/\/yykcj.com\/?p=322"},"modified":"2026-11-20T12:51:40","modified_gmt":"2026-11-20T04:51:40","slug":"htmx-%e4%b8%8e%e5%8f%a4%e5%85%b8-web-%e5%bc%80%e5%8f%91%e5%a4%8d%e5%85%b4%ef%bc%9a%e5%91%8a%e5%88%ab%e5%a4%8d%e6%9d%82%e5%89%8d%e7%ab%af%ef%bc%8c%e7%94%a8-html-%e9%a9%b1%e5%8a%a8%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=322","title":{"rendered":"HTMX \u4e0e\u53e4\u5178 Web \u5f00\u53d1\u590d\u5174\uff1a\u544a\u522b\u590d\u6742\u524d\u7aef\uff0c\u7528 HTML \u9a71\u52a8\u4ea4\u4e92"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981 HTMX\uff1f<\/h2>\n<p>\u8fc7\u53bb\u5341\u5e74\uff0c\u524d\u7aef\u5f00\u53d1\u9010\u6e10\u4ece\u670d\u52a1\u7aef\u6e32\u67d3\uff08SSR\uff09\u8f6c\u5411\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u3002React\u3001Vue \u7b49\u6846\u67b6\u5e26\u6765\u4e86\u7ec4\u4ef6\u5316\u3001\u54cd\u5e94\u5f0f\u7b49\u4f18\u52bf\uff0c\u4f46\u4e5f\u5f15\u5165\u4e86\u6784\u5efa\u5de5\u5177\u3001\u72b6\u6001\u7ba1\u7406\u3001\u8def\u7531\u7b49\u590d\u6742\u6027\u3002\u5bf9\u4e8e\u8bb8\u591a\u4ee5\u5185\u5bb9\u5c55\u793a\u548c\u8868\u5355\u4ea4\u4e92\u4e3a\u4e3b\u7684 Web \u5e94\u7528\uff0c\u8fd9\u4e9b\u590d\u6742\u6027\u5f80\u5f80\u5f97\u4e0d\u507f\u5931\u3002<\/p>\n<p>HTMX \u7684\u51fa\u73b0\u91cd\u65b0\u70b9\u71c3\u4e86\u201c\u53e4\u5178 Web \u5f00\u53d1\u201d\u7684\u706b\u79cd\uff1a\u5b83\u901a\u8fc7\u6269\u5c55 HTML \u5c5e\u6027\uff0c\u8ba9\u4efb\u4f55\u5143\u7d20\u90fd\u80fd\u53d1\u8d77 AJAX \u8bf7\u6c42\u3001\u5c40\u90e8\u66f4\u65b0\u9875\u9762\uff0c\u800c\u65e0\u9700\u7f16\u5199 JavaScript\u3002\u540e\u7aef\u4ecd\u7136\u8d1f\u8d23\u6e32\u67d3 HTML\uff0c\u524d\u7aef\u53ea\u8d1f\u8d23\u5c55\u793a\u548c\u4ea4\u4e92\u3002\u8fd9\u79cd\u6a21\u5f0f\u88ab\u79f0\u4e3a\u201c\u8d85\u5a92\u4f53\u9a71\u52a8\u201d\uff08Hypermedia-Driven\uff09\u5e94\u7528\uff0c\u5b83\u964d\u4f4e\u4e86\u524d\u540e\u7aef\u5206\u79bb\u5e26\u6765\u7684\u8ba4\u77e5\u8d1f\u62c5\uff0c\u63d0\u5347\u4e86\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p>\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u5f00\u59cb\uff0c\u7528 HTMX \u548c Go \u6784\u5efa\u4e00\u4e2a\u4efb\u52a1\u7ba1\u7406\u5e94\u7528\uff0c\u4f53\u9a8c\u53e4\u5178 Web \u5f00\u53d1\u7684\u590d\u5174\u3002<\/p>\n<h2>\u73af\u5883\u51c6\u5907<\/h2>\n<p>\u4e3a\u4e86\u4e13\u6ce8\u4e8e HTMX\uff0c\u6211\u4eec\u9009\u62e9 Go \u4f5c\u4e3a\u540e\u7aef\u8bed\u8a00\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9002\u5408\u5feb\u901f\u6784\u5efa HTML \u670d\u52a1\u3002\u4f60\u9700\u8981\u5b89\u88c5\uff1a<\/p>\n<ul>\n<li>Go 1.20+\uff08<a href=\"https:\/\/go.dev\/dl\/\">\u4e0b\u8f7d\u5730\u5740<\/a>\uff09<\/li>\n<li>\u4efb\u610f\u6587\u672c\u7f16\u8f91\u5668<\/li>\n<\/ul>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\u5e76\u521d\u59cb\u5316 Go \u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-bash\">\nmkdir htmx-todo &amp;&amp; cd htmx-todo\ngo mod init example.com\/htmx-todo\n<\/code><\/pre>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u6807\u51c6\u5e93 <code>net\/http<\/code> \u548c <code>html\/template<\/code>\uff0c\u65e0\u9700\u989d\u5916\u4f9d\u8d56\u3002<\/p>\n<h2>\u9879\u76ee\u7ed3\u6784<\/h2>\n<pre><code>\nhtmx-todo\/\n\u251c\u2500\u2500 main.go\n\u251c\u2500\u2500 templates\/\n\u2502   \u251c\u2500\u2500 index.html\n\u2502   \u2514\u2500\u2500 tasks.html\n\u2514\u2500\u2500 go.mod\n<\/code><\/pre>\n<h2>\u7b2c\u4e00\u6b65\uff1a\u642d\u5efa\u57fa\u7840\u670d\u52a1<\/h2>\n<p>\u521b\u5efa <code>main.go<\/code>\uff0c\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684 HTTP \u670d\u52a1\u5668\uff0c\u5e76\u6e32\u67d3\u9996\u9875\u3002<\/p>\n<pre><code class=\"language-go\">\npackage main\n\nimport (\n    &quot;html\/template&quot;\n    &quot;log&quot;\n    &quot;net\/http&quot;\n)\n\nfunc main() {\n    http.HandleFunc(&quot;\/&quot;, indexHandler)\n    log.Println(&quot;Server started on :8080&quot;)\n    log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))\n}\n\nfunc indexHandler(w http.ResponseWriter, r *http.Request) {\n    tmpl := template.Must(template.ParseFiles(&quot;templates\/index.html&quot;))\n    tmpl.Execute(w, nil)\n}\n<\/code><\/pre>\n<p>\u5728 <code>templates\/index.html<\/code> \u4e2d\u5f15\u5165 HTMX\uff08\u901a\u8fc7 CDN\uff09\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;HTMX Todo&lt;\/title&gt;\n    &lt;script src=&quot;https:\/\/unpkg.com\/htmx.org@1.9.10&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;\u4efb\u52a1\u5217\u8868&lt;\/h1&gt;\n    &lt;div id=&quot;task-list&quot;&gt;\n        &lt;!-- \u4efb\u52a1\u5217\u8868\u5c06\u5728\u8fd9\u91cc\u6e32\u67d3 --&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u8fd0\u884c <code>go run main.go<\/code>\uff0c\u8bbf\u95ee <code>http:\/\/localhost:8080<\/code>\uff0c\u4f60\u5e94\u8be5\u80fd\u770b\u5230\u7a7a\u767d\u9875\u9762\u3002<\/p>\n<h2>\u7b2c\u4e8c\u6b65\uff1a\u5b9a\u4e49\u6570\u636e\u6a21\u578b\u548c\u4efb\u52a1\u5217\u8868<\/h2>\n<p>\u4e3a\u4e86\u7b80\u5355\uff0c\u6211\u4eec\u4f7f\u7528\u5185\u5b58\u5b58\u50a8\u4efb\u52a1\u3002\u5728 <code>main.go<\/code> \u4e2d\u6dfb\u52a0\uff1a<\/p>\n<pre><code class=\"language-go\">\ntype Task struct {\n    ID   int\n    Name string\n}\n\nvar tasks = []Task{\n    {ID: 1, Name: &quot;\u5b66\u4e60 HTMX&quot;},\n    {ID: 2, Name: &quot;\u7f16\u5199\u6559\u7a0b&quot;},\n}\nvar nextID = 3\n<\/code><\/pre>\n<p>\u4fee\u6539 <code>indexHandler<\/code>\uff0c\u5c06\u4efb\u52a1\u5217\u8868\u4f20\u9012\u7ed9\u6a21\u677f\uff1a<\/p>\n<pre><code class=\"language-go\">\nfunc indexHandler(w http.ResponseWriter, r *http.Request) {\n    tmpl := template.Must(template.ParseFiles(&quot;templates\/index.html&quot;))\n    tmpl.Execute(w, tasks)\n}\n<\/code><\/pre>\n<p>\u66f4\u65b0 <code>index.html<\/code>\uff0c\u4f7f\u7528 Go \u6a21\u677f\u6e32\u67d3\u4efb\u52a1\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;div id=&quot;task-list&quot;&gt;\n    {{ range . }}\n        &lt;div class=&quot;task&quot;&gt;{{ .Name }}&lt;\/div&gt;\n    {{ end }}\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u73b0\u5728\u5237\u65b0\u9875\u9762\uff0c\u4f60\u4f1a\u770b\u5230\u4e24\u4e2a\u4efb\u52a1\u3002<\/p>\n<h2>\u7b2c\u4e09\u6b65\uff1a\u7528 HTMX \u5b9e\u73b0\u6dfb\u52a0\u4efb\u52a1<\/h2>\n<p>HTMX \u7684\u6838\u5fc3\u662f <code>hx-*<\/code> \u5c5e\u6027\u3002\u6211\u4eec\u6dfb\u52a0\u4e00\u4e2a\u8868\u5355\uff0c\u901a\u8fc7 <code>hx-post<\/code> \u63d0\u4ea4\u5230 <code>\/tasks<\/code>\uff0c\u5e76\u6307\u5b9a <code>hx-target<\/code> \u4e3a\u4efb\u52a1\u5217\u8868\u5bb9\u5668\uff0c<code>hx-swap<\/code> \u4e3a <code>beforeend<\/code>\uff0c\u8fd9\u6837\u65b0\u4efb\u52a1\u4f1a\u8ffd\u52a0\u5230\u5217\u8868\u672b\u5c3e\u3002<\/p>\n<p>\u5728 <code>index.html<\/code> \u4e2d\u6dfb\u52a0\u8868\u5355\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;form hx-post=&quot;\/tasks&quot; hx-target=&quot;#task-list&quot; hx-swap=&quot;beforeend&quot;&gt;\n    &lt;input type=&quot;text&quot; name=&quot;name&quot; placeholder=&quot;\u65b0\u4efb\u52a1...&quot; required&gt;\n    &lt;button type=&quot;submit&quot;&gt;\u6dfb\u52a0&lt;\/button&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>\u540e\u7aef\u9700\u8981\u5904\u7406 <code>POST \/tasks<\/code>\uff0c\u89e3\u6790\u8868\u5355\u6570\u636e\uff0c\u521b\u5efa\u4efb\u52a1\uff0c\u5e76\u8fd4\u56de\u65b0\u4efb\u52a1\u7684 HTML \u7247\u6bb5\uff08\u800c\u4e0d\u662f\u6574\u4e2a\u9875\u9762\uff09\u3002<\/p>\n<pre><code class=\"language-go\">\nfunc addTaskHandler(w http.ResponseWriter, r *http.Request) {\n    if r.Method != http.MethodPost {\n        http.Error(w, &quot;Method not allowed&quot;, http.StatusMethodNotAllowed)\n        return\n    }\n    name := r.FormValue(&quot;name&quot;)\n    if name == &quot;&quot; {\n        http.Error(w, &quot;\u4efb\u52a1\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a&quot;, http.StatusBadRequest)\n        return\n    }\n    task := Task{ID: nextID, Name: name}\n    nextID++\n    tasks = append(tasks, task)\n\n    \/\/ \u8fd4\u56de\u65b0\u4efb\u52a1\u7684 HTML \u7247\u6bb5\n    tmpl := template.Must(template.ParseFiles(&quot;templates\/task.html&quot;))\n    tmpl.Execute(w, task)\n}\n<\/code><\/pre>\n<p>\u521b\u5efa <code>templates\/task.html<\/code>\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;div class=&quot;task&quot;&gt;{{ .Name }}&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u5728 <code>main.go<\/code> \u4e2d\u6ce8\u518c\u8def\u7531\uff1a<\/p>\n<pre><code class=\"language-go\">\nhttp.HandleFunc(&quot;\/tasks&quot;, addTaskHandler)\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4\uff0c\u65b0\u4efb\u52a1\u4f1a\u7acb\u5373\u51fa\u73b0\u5728\u5217\u8868\u4e2d\uff0c\u9875\u9762\u4e0d\u4f1a\u5237\u65b0\u3002<\/p>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1aHTMX \u9ed8\u8ba4\u4f1a\u5904\u7406\u8868\u5355\u7684 <code>submit<\/code> \u4e8b\u4ef6\uff0c\u5e76\u963b\u6b62\u9ed8\u8ba4\u884c\u4e3a\u3002\u786e\u4fdd\u8868\u5355\u7684 <code>method<\/code> \u4e0d\u662f <code>GET<\/code>\uff08\u56e0\u4e3a HTMX \u4f1a\u4f7f\u7528 <code>hx-post<\/code> \u6307\u5b9a\u7684\u65b9\u6cd5\uff09\uff0c\u6216\u8005\u663e\u5f0f\u8bbe\u7f6e <code>hx-post<\/code>\u3002<\/p>\n<h2>\u7b2c\u56db\u6b65\uff1a\u5b9e\u73b0\u5220\u9664\u4efb\u52a1<\/h2>\n<p>\u4e3a\u6bcf\u4e2a\u4efb\u52a1\u6dfb\u52a0\u5220\u9664\u6309\u94ae\u3002\u5728 <code>task.html<\/code> \u4e2d\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;div class=&quot;task&quot;&gt;\n    {{ .Name }}\n    &lt;button hx-delete=&quot;\/tasks\/{{ .ID }}&quot; hx-target=&quot;closest .task&quot; hx-swap=&quot;outerHTML swap:1s&quot;&gt;\u5220\u9664&lt;\/button&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u8fd9\u91cc <code>hx-delete<\/code> \u53d1\u9001 DELETE \u8bf7\u6c42\uff0c<code>hx-target=\"closest .task\"<\/code> \u8868\u793a\u76ee\u6807\u5143\u7d20\u662f\u6700\u8fd1\u7684 <code>.task<\/code> \u7956\u5148\uff0c<code>hx-swap=\"outerHTML swap:1s\"<\/code> \u8868\u793a\u7528\u54cd\u5e94\u66ff\u6362\u6574\u4e2a\u5143\u7d20\uff0c\u5e76\u7b49\u5f85 1 \u79d2\u540e\u79fb\u9664\uff08\u7528\u4e8e\u52a8\u753b\uff09\u3002<\/p>\n<p>\u540e\u7aef\u5904\u7406 <code>DELETE \/tasks\/{id}<\/code>\uff1a<\/p>\n<pre><code class=\"language-go\">\nfunc deleteTaskHandler(w http.ResponseWriter, r *http.Request) {\n    if r.Method != http.MethodDelete {\n        http.Error(w, &quot;Method not allowed&quot;, http.StatusMethodNotAllowed)\n        return\n    }\n    idStr := r.URL.Path[len(&quot;\/tasks\/&quot;):]\n    id, err := strconv.Atoi(idStr)\n    if err != nil {\n        http.Error(w, &quot;\u65e0\u6548\u7684\u4efb\u52a1 ID&quot;, http.StatusBadRequest)\n        return\n    }\n    for i, task := range tasks {\n        if task.ID == id {\n            tasks = append(tasks[:i], tasks[i+1:]...)\n            break\n        }\n    }\n    w.WriteHeader(http.StatusOK) \/\/ \u8fd4\u56de\u7a7a\u54cd\u5e94\uff0cHTMX \u4f1a\u79fb\u9664\u76ee\u6807\u5143\u7d20\n}\n<\/code><\/pre>\n<p>\u6ce8\u518c\u8def\u7531\uff1a<\/p>\n<pre><code class=\"language-go\">\nhttp.HandleFunc(&quot;\/tasks\/&quot;, deleteTaskHandler)\n<\/code><\/pre>\n<p>\u73b0\u5728\u70b9\u51fb\u5220\u9664\u6309\u94ae\uff0c\u4efb\u52a1\u4f1a\u6de1\u51fa\u5e76\u6d88\u5931\u3002<\/p>\n<h2>\u7b2c\u4e94\u6b65\uff1a\u6dfb\u52a0\u52a0\u8f7d\u6307\u793a\u5668\u548c\u9519\u8bef\u5904\u7406<\/h2>\n<p>HTMX \u63d0\u4f9b\u4e86 <code>hx-indicator<\/code> \u5c5e\u6027\u6765\u663e\u793a\u52a0\u8f7d\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5728\u8868\u5355\u63d0\u4ea4\u65f6\u663e\u793a\u4e00\u4e2a\u52a0\u8f7d\u56fe\u6807\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;form hx-post=&quot;\/tasks&quot; hx-target=&quot;#task-list&quot; hx-swap=&quot;beforeend&quot; hx-indicator=&quot;#spinner&quot;&gt;\n    &lt;input type=&quot;text&quot; name=&quot;name&quot; placeholder=&quot;\u65b0\u4efb\u52a1...&quot; required&gt;\n    &lt;button type=&quot;submit&quot;&gt;\u6dfb\u52a0&lt;\/button&gt;\n    &lt;span id=&quot;spinner&quot; class=&quot;htmx-indicator&quot;&gt;\u52a0\u8f7d\u4e2d...&lt;\/span&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>\u6dfb\u52a0\u4e00\u70b9 CSS \u8ba9\u6307\u793a\u5668\u9ed8\u8ba4\u9690\u85cf\uff1a<\/p>\n<pre><code class=\"language-css\">\n.htmx-indicator { display: none; }\n.htmx-request .htmx-indicator { display: inline; }\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u9519\u8bef\u5904\u7406\uff0cHTMX \u9ed8\u8ba4\u4e0d\u4f1a\u66ff\u6362\u76ee\u6807\u5185\u5bb9\u5982\u679c\u54cd\u5e94\u72b6\u6001\u7801\u4e0d\u662f 2xx\u3002\u4f60\u53ef\u4ee5\u76d1\u542c <code>htmx:responseError<\/code> \u4e8b\u4ef6\u6765\u663e\u793a\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;script&gt;\ndocument.body.addEventListener(&#x27;htmx:responseError&#x27;, function(evt) {\n    alert(&#x27;\u8bf7\u6c42\u5931\u8d25: &#x27; + evt.detail.xhr.status);\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n<h2>\u7b2c\u516d\u6b65\uff1a\u4f7f\u7528 HTMX \u6269\u5c55\u589e\u5f3a\u4f53\u9a8c<\/h2>\n<p>HTMX \u5b98\u65b9\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6269\u5c55\uff0c\u6bd4\u5982 <code>json-enc<\/code> \u7528\u4e8e\u53d1\u9001 JSON \u6570\u636e\uff0c<code>loading-states<\/code> \u7528\u4e8e\u7ba1\u7406\u52a0\u8f7d\u72b6\u6001\u3002\u6211\u4eec\u4ee5 <code>loading-states<\/code> \u4e3a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7981\u7528\u6309\u94ae\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u3002<\/p>\n<p>\u5f15\u5165\u6269\u5c55\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;script src=&quot;https:\/\/unpkg.com\/htmx.org@1.9.10\/dist\/ext\/loading-states.js&quot;&gt;&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u5728\u8868\u5355\u4e0a\u6dfb\u52a0 <code>hx-ext=\"loading-states\"<\/code>\uff0c\u5e76\u5728\u6309\u94ae\u4e0a\u4f7f\u7528 <code>data-loading-disable<\/code>\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;form hx-post=&quot;\/tasks&quot; hx-target=&quot;#task-list&quot; hx-swap=&quot;beforeend&quot; hx-ext=&quot;loading-states&quot;&gt;\n    &lt;input type=&quot;text&quot; name=&quot;name&quot; placeholder=&quot;\u65b0\u4efb\u52a1...&quot; required&gt;\n    &lt;button type=&quot;submit&quot; data-loading-disable&gt;\u6dfb\u52a0&lt;\/button&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5728\u8bf7\u6c42\u671f\u95f4\u6309\u94ae\u4f1a\u88ab\u7981\u7528\uff0c\u907f\u514d\u91cd\u590d\u63d0\u4ea4\u3002<\/p>\n<h2>\u6700\u4f73\u5b9e\u8df5\u4e0e\u8e29\u5751\u7ecf\u9a8c<\/h2>\n<ol>\n<li><strong>\u4fdd\u6301\u54cd\u5e94\u7247\u6bb5\u7cbe\u7b80<\/strong>\uff1aHTMX \u671f\u671b\u670d\u52a1\u5668\u8fd4\u56de HTML \u7247\u6bb5\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u9875\u9762\u3002\u786e\u4fdd\u4f60\u7684\u5904\u7406\u7a0b\u5e8f\u53ea\u6e32\u67d3\u5fc5\u8981\u7684\u90e8\u5206\uff0c\u51cf\u5c11\u4f20\u8f93\u6570\u636e\u91cf\u3002<\/li>\n<\/ol>\n<ol>\n<li><strong>\u4f7f\u7528 <code>hx-target<\/code> \u548c <code>hx-swap<\/code> \u7cbe\u786e\u63a7\u5236\u66f4\u65b0<\/strong>\uff1a\u907f\u514d\u610f\u5916\u66ff\u6362\u6574\u4e2a\u9875\u9762\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u662f\u4f7f\u7528 <code>hx-target=\"#container\"<\/code> \u548c <code>hx-swap=\"innerHTML\"<\/code>\u3002<\/li>\n<\/ol>\n<ol>\n<li><strong>\u6ce8\u610f CSRF \u4fdd\u62a4<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u5e94\u7528\u9700\u8981 CSRF \u4fdd\u62a4\uff0c\u53ef\u4ee5\u5728 HTMX \u8bf7\u6c42\u4e2d\u81ea\u52a8\u6dfb\u52a0 token\u3002\u901a\u8fc7 <code>hx-headers<\/code> \u5c5e\u6027\u6216\u5168\u5c40\u4e8b\u4ef6\u8bbe\u7f6e\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-html\">\n&lt;body hx-headers=&#x27;{&quot;X-CSRF-Token&quot;: &quot;{{ .CSRFToken }}&quot;}&#x27;&gt;\n<\/code><\/pre>\n<ol>\n<li><strong>\u5904\u7406 3xx \u91cd\u5b9a\u5411<\/strong>\uff1aHTMX \u9ed8\u8ba4\u4f1a\u8ddf\u968f\u91cd\u5b9a\u5411\uff0c\u4f46\u5982\u679c\u4f60\u5e0c\u671b\u5ba2\u6237\u7aef\u5904\u7406\u91cd\u5b9a\u5411\uff0c\u53ef\u4ee5\u8bbe\u7f6e <code>hx-redirect<\/code> \u54cd\u5e94\u5934\u3002<\/li>\n<\/ol>\n<ol>\n<li><strong>\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528 HTMX<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684\u5ba2\u6237\u7aef\u72b6\u6001\uff08\u5982\u62d6\u62fd\u6392\u5e8f\u3001\u5bcc\u6587\u672c\u7f16\u8f91\u5668\uff09\uff0c\u4ecd\u7136\u9700\u8981 JavaScript\u3002HTMX \u4e0d\u662f\u94f6\u5f39\uff0c\u800c\u662f\u7b80\u5316\u5e38\u89c1\u4ea4\u4e92\u7684\u5de5\u5177\u3002<\/li>\n<\/ol>\n<ol>\n<li><strong>\u8c03\u8bd5\u6280\u5de7<\/strong>\uff1a\u5728\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u8f93\u5165 <code>htmx.logAll()<\/code> \u53ef\u4ee5\u67e5\u770b\u6240\u6709 HTMX \u4e8b\u4ef6\u548c\u8bf7\u6c42\uff0c\u65b9\u4fbf\u6392\u67e5\u95ee\u9898\u3002<\/li>\n<\/ol>\n<h2>\u603b\u7ed3<\/h2>\n<p>HTMX \u8ba9\u6211\u4eec\u91cd\u65b0\u5ba1\u89c6 Web \u5f00\u53d1\u7684\u672c\u8d28\uff1a\u8d85\u5a92\u4f53\u5373\u72b6\u6001\u3002\u901a\u8fc7\u5c06\u4ea4\u4e92\u903b\u8f91\u653e\u56de\u670d\u52a1\u7aef\uff0c\u6211\u4eec\u51cf\u5c11\u4e86\u524d\u540e\u7aef\u7684\u91cd\u590d\u4ee3\u7801\uff0c\u964d\u4f4e\u4e86\u6784\u5efa\u590d\u6742\u5ea6\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u4efb\u52a1\u7ba1\u7406\u793a\u4f8b\uff0c\u5c55\u793a\u4e86 HTMX \u7684\u6838\u5fc3\u7528\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\u3002\u4f60\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u7ee7\u7eed\u63a2\u7d22\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>hx-trigger<\/code> \u81ea\u5b9a\u4e49\u89e6\u53d1\u4e8b\u4ef6\uff08\u5982 <code>keyup changed delay:500ms<\/code> \u5b9e\u73b0\u641c\u7d22\uff09<\/li>\n<li>\u7ed3\u5408 WebSocket \u5b9e\u73b0\u5b9e\u65f6\u66f4\u65b0<\/li>\n<li>\u63a2\u7d22\u5176\u4ed6\u540e\u7aef\u8bed\u8a00\uff08\u5982 Python Flask\u3001Ruby on Rails\uff09\u4e0e HTMX \u7684\u96c6\u6210<\/li>\n<\/ul>\n<p>\u53e4\u5178 Web \u5f00\u53d1\u5e76\u672a\u8fc7\u65f6\uff0cHTMX \u4e3a\u5176\u6ce8\u5165\u4e86\u65b0\u7684\u6d3b\u529b\u3002\u5c1d\u8bd5\u5728\u4f60\u7684\u4e0b\u4e00\u4e2a\u9879\u76ee\u4e2d\u7528 HTMX \u66ff\u4ee3\u90e8\u5206\u524d\u7aef\u6846\u67b6\uff0c\u6216\u8bb8\u4f60\u4f1a\u53d1\u73b0\uff0c\u7b80\u5355\u624d\u662f\u7ec8\u6781\u7684\u590d\u6742\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/htmx.org\/docs\/\">HTMX \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/hypermedia.systems\/\">Hypermedia Systems \u4e66\u7c4d<\/a>\uff08\u514d\u8d39\u5728\u7ebf\uff09<\/li>\n<li><a href=\"https:\/\/martinfowler.com\/articles\/richardsonMaturityModel.html\">HATEOAS \u4e0e REST \u6210\u719f\u5ea6\u6a21\u578b<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd HTMX \u5982\u4f55\u8ba9\u5f00\u53d1\u8005\u56de\u5f52\u670d\u52a1\u7aef\u6e32\u67d3\uff0c\u7528 HTML \u5c5e\u6027\u5b9e\u73b0\u52a8\u6001\u4ea4\u4e92\uff0c\u907f\u514d SPA \u7684\u590d\u6742\u6027\u3002\u901a\u8fc7\u5b8c\u6574\u793a\u4f8b\u6f14\u793a\u4ece\u96f6\u6784\u5efa\u4e00\u4e2a\u4efb\u52a1\u7ba1\u7406\u5e94\u7528\uff0c\u5e76\u5206\u4eab\u5b9e\u6218\u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u4e0e\u8e29\u5751\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[92,291,287,293,292],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-go","tag-htmx","tag-web","tag-293","tag-292"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/322\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}