日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

不使用 Javascript 也可以和瀏覽器進行交互?

 

Htmx 是一個庫,它允許你直接從 html 訪問現(xiàn)代瀏覽器功能,而不是使用 JAVAscript。

不使用 Javascript 也可以和瀏覽器進行交互?

 

要理解 htmx,首先讓我們看一下 HTML 中的 a 標簽:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a href="/blog">Blog</a></pre>

這個標記會告訴瀏覽器:當用戶單擊此鏈接時,向 /blog 發(fā)出 HTTP GET 請求并將響應內(nèi)容加載到瀏覽器窗口中。

然后我們再看下面的 HTML:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><button hx-post="/clicked"
    hx-trigger="click"
    hx-target="#parent-div"
    hx-swap="outerHTML"
>
    Click Me!
</button></pre>

這告訴 htmx:當用戶單擊此按鈕時,向 /clicked 發(fā)出 HTTP POST 請求并使用響應中的內(nèi)容將元素替換為 id 為 parent-div 的 DOM。

Htmx 將 HTML 的核心思想進行了擴展,為 HTML 語言提供了更多可能性:

  • 現(xiàn)在任何元素,不僅僅是超鏈接和表單,都可以發(fā)出 HTTP 請求。
  • 現(xiàn)在任何事件,不僅僅是點擊或表單提交,都可以觸發(fā)請求。
  • 現(xiàn)在可以使用任何 HTTP verb,而不僅僅是GET 和POST。
  • 現(xiàn)在任何元素,不僅僅是整個window 對象,都可以成為請求更新的目標。

請注意,當你使用 htmx 時,在服務器端你通常會使用 HTML 而非 JSON 進行響應。這會讓你使用原始 Web 編程模型,使用超文本作為應用程序狀態(tài)的引擎,甚至你也不需要真正理解這個概念。

另外如果你愿意,可以在使用 htmx 時使用 data- 前綴:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a data-hx-post="/click">Click Me!</a></pre>

安裝

Htmx 是一個無依賴、面向瀏覽器的 JavaScript 庫。這意味著使用它就像在文檔頭部添加一個 <script> 標記一樣簡單,無需復雜的構建步驟或系統(tǒng)。

通過 CDN

使用 htmx 的最快方法是通過 CDN 加載它。你可以簡單地將其添加到你的 head 標簽中即可:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><script src="https://unpkg.com/[email protected]" integrity="sha384-cZuAZ+ZbwkNRnrKi05G/fjBX+azI9DNOkNYysZ0I/X5ZFgsmMiBXgDZof30F5ofc" crossorigin="anonymous"></script></pre>

npm

對于 npm 風格的構建系統(tǒng),同樣你可以通過 npm 安裝 htmx:

<pre class="prettyprint hljs Nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">npm install htmx.org</pre>

安裝后,你需要使用適當?shù)墓ぞ邅硎褂?br />node_modules/htmx.org/dist/htmx.js(或 .min.js),例如你可以將 htmx 與一些擴展和特定于項目的代碼捆綁在一起。

如果你使用 webpack 來管理你的 javascript:

  • 通過你最喜歡的包管理器(如 npm 或 yarn)安裝 htmx。
  • 在你的index.js 文件 import htmx。
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">import 'htmx.org';</pre>

如果要使用全局 htmx 變量(推薦),則需要將其注入到 window 作用域中:

  • 創(chuàng)建一個定制的 JS 文件。
  • 在index.js 文件中導入該文件。
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">import 'path/to/my_custom.js';</pre>
  • 然后添加如下所示的代碼:
<pre class="prettyprint hljs javascript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">window.htmx = require('htmx.org');</pre>
  • 最后重新打包即可

AJAX

htmx 的核心是一組允許你直接從 HTML 發(fā)出 AJAX 請求的屬性:

  • hx-get:向指定的 URL 發(fā)出 GET 請求。
  • hx-post:向指定的 URL 發(fā)出 POST 請求。
  • hx-put:向指定的 URL 發(fā)出 PUT 請求。
  • hx-patch:向指定的 URL 發(fā)出 PATCH 請求。
  • hx-delete:向指定的 URL 發(fā)出 DELETE 請求。

這些屬性都需要一個 URL 來向其發(fā)出 AJAX 請求,當元素被觸發(fā)時,該元素將向給定的 URL 發(fā)出指定類型的請求:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div hx-put="/messages">
    Put To Messages
</div></pre>

這會告訴瀏覽器:當用戶點擊此 div 時,向 /messages 發(fā)出 PUT 請求并將響應加載到 div。

觸發(fā)請求

默認情況下,AJAX 請求由元素的 自然 事件觸發(fā):

  • input、textarea? 和select? 在change 事件上觸發(fā)。
  • form 在提交事件上觸發(fā)。
  • 其他元素都由click 事件觸發(fā)。

如果你想要不同的行為,可以使用 hx-trigger 屬性來指定哪個事件將觸發(fā)請求。

比如下面的一段代碼表示在鼠標進入時觸發(fā)到 /mouse_entered 的 POST 請求:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div hx-post="/mouse_entered" hx-trigger="mouseenter">
    [Here Mouse, Mouse!]
</div></pre>

HTMX 還有很多使用的方式,可以前往官方文檔 https://htmx.org/docs/ 了解更多。

示例

下面我們用幾個示例來簡單說明下 htmx 是如何使用的。

點擊加載數(shù)據(jù)

這個例子展示了如何在數(shù)據(jù)表格中實現(xiàn)點擊加載下一頁,關鍵是最后一行:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><tr id="replaceMe">
  <td colspan="3">
    <button class='btn' hx-get="/contacts/?page=2" 
                        hx-target="#replaceMe" 
                        hx-swap="outerHTML">
         Load More Agents... <img class="htmx-indicator" src="/img/bars.svg">
    </button>
  </td>
</tr></pre>

該行包含一個按鈕,該按鈕將用下一頁結(jié)果替換整行(其中將包含一個用于加載下一頁結(jié)果的按鈕)。

不使用 Javascript 也可以和瀏覽器進行交互?

 

當點擊 Load More Agents... 按鈕后會加載一頁數(shù)據(jù)附加到表格中去。

延遲加載

這個例子展示了如何在頁面上延遲加載元素。我們從如下所示的初始狀態(tài)開始:

<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div hx-get="/graph" hx-trigger="load">
  <img  alt="Result loading..." class="htmx-indicator" width="150" src="/img/bars.svg"/>
</div></pre>

當我們加載圖表時,它會顯示一個進度指示器,然后通過 css 過渡加載圖表并逐漸淡入視圖:

<pre class="prettyprint hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">.htmx-settling img {
  opacity: 0;
}
img {
 transition: opacity 300ms ease-in;
}</pre>

該示例的效果就是先顯示一個加載的指示器,然后加載一張圖片出來,就是通常的延遲加載的效果。

不使用 Javascript 也可以和瀏覽器進行交互?

 


不使用 Javascript 也可以和瀏覽器進行交互?

 

Git 倉庫:
https://github.com/bigskysoftware/htmx。

來源:
https://developer.51cto.com/article/714198.html

分享到:
標簽:瀏覽器
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定