文学起点网
当前位置: 首页 文学百科

javascript与前端连接(不使用Javascript也可以和浏览器进行交互)

时间:2023-06-04 作者: 小编 阅读量: 8 栏目名: 文学百科

现在任何事件,不仅仅是点击或表单提交,都可以触发请求。现在可以使用任何HTTPverb,而不仅仅是GET和POST。现在任何元素,不仅仅是整个window对象,都可以成为请求更新的目标。请注意,当你使用htmx时,在服务器端你通常会使用HTML而非JSON进行响应。</a></pre>安装Htmx是一个无依赖、面向浏览器的JavaScript库。这意味着使用它就像在文档头部添加一个<script>标记一样简单,无需复杂的构建步骤或系统。通过CDN使用htmx的最快方法是通过CDN加载它。

javascript与前端连接?Htmx 是一个库,它允许你直接从 HTML 访问现代浏览器功能,而不是使用 javascript,我来为大家科普一下关于javascript与前端连接?以下内容希望对你有帮助!

javascript与前端连接

Htmx 是一个库,它允许你直接从 HTML 访问现代浏览器功能,而不是使用 javascript。

要理解 htmx,首先让我们看一下 HTML 中的 a 标签:

<preCourier 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 发出 HTTP GET 请求并将响应内容加载到浏览器窗口中。

然后我们再看下面的 HTML:

<preCourier 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 发出 HTTP POST 请求并使用响应中的内容将元素替换为 id 为 parent-div 的 DOM。

Htmx 将 HTML 的核心思想进行了扩展,为 HTML 语言提供了更多可能性:

  • 现在任何元素,不仅仅是超链接和表单,都可以发出 HTTP 请求。
  • 现在任何事件,不仅仅是点击或表单提交,都可以触发请求。
  • 现在可以使用任何 HTTP verb,而不仅仅是GET 和POST。
  • 现在任何元素,不仅仅是整个window 对象,都可以成为请求更新的目标。

    请注意,当你使用 htmx 时,在服务器端你通常会使用 HTML 而非 JSON 进行响应。这会让你使用原始 Web 编程模型,使用超文本作为应用程序状态的引擎,甚至你也不需要真正理解这个概念。

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

    <preCourier 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> 标记一样简单,无需复杂的构建步骤或系统。

    通过 CDN

    使用 htmx 的最快方法是通过 CDN 加载它。你可以简单地将其添加到你的 head 标签中即可:

    <preCourier 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="http://unpkg.com/htmx.org@1.8.0" integrity="sha384-cZuAZ ZbwkNRnrKi05G/fjBX azI9DNOkNYysZ0I/X5ZFgsmMiBXgDZof30F5ofc" crossorigin="anonymous"></script></pre>

    npm

    对于 npm 风格的构建系统,同样你可以通过 npm 安装 htmx:

    <preCourier 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>

    安装后,你需要使用适当的工具来使用 node_modules/htmx.org/dist/htmx.js(或 .min.js),例如你可以将 htmx 与一些扩展和特定于项目的代码捆绑在一起。

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

  • 通过你最喜欢的包管理器(如 npm 或 yarn)安装 htmx。
  • 在你的index.js 文件 import htmx。

    <preCourier 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 作用域中:

  • 创建一个定制的 JS 文件。
  • 在index.js 文件中导入该文件。

    <preCourier 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>

  • 然后添加如下所示的代码:

    <preCourier 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 发出 AJAX 请求的属性:

  • hx-get:向指定的 URL 发出 GET 请求。
  • hx-post:向指定的 URL 发出 POST 请求。
  • hx-put:向指定的 URL 发出 PUT 请求。
  • hx-patch:向指定的 URL 发出 PATCH 请求。
  • hx-delete:向指定的 URL 发出 DELETE 请求。

    这些属性都需要一个 URL 来向其发出 AJAX 请求,当元素被触发时,该元素将向给定的 URL 发出指定类型的请求:

    <preCourier 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 发出 PUT 请求并将响应加载到 div。

    触发请求

    默认情况下,AJAX 请求由元素的 自然 事件触发:

  • input、textarea​ 和select​ 在change 事件上触发。
  • form 在提交事件上触发。
  • 其他元素都由click 事件触发。

    如果你想要不同的行为,可以使用 hx-trigger 属性来指定哪个事件将触发请求。

    比如下面的一段代码表示在鼠标进入时触发到 /mouse_entered 的 POST 请求:

    <preCourier 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 还有很多使用的方式,可以前往官方文档 http://htmx.org/docs/ 了解更多。

    示例

    下面我们用几个示例来简单说明下 htmx 是如何使用的。

    点击加载数据

    这个例子展示了如何在数据表格中实现点击加载下一页,关键是最后一行:

    <preCourier 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 ><td colspan="3"><button class='btn' hx-get="/contacts/?page=2"hx-target="#replaceMe"hx-swap="outerHTML">Load More Agents... <imgsrc="/img/bars.svg"></button></td></tr></pre>

    该行包含一个按钮,该按钮将用下一页结果替换整行(其中将包含一个用于加载下一页结果的按钮)。

    当点击 Load More Agents... 按钮后会加载一页数据附加到表格中去。

    延迟加载

    这个例子展示了如何在页面上延迟加载元素。我们从如下所示的初始状态开始:

    <preCourier 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"><imgsrc="/img/bars.svg"/></div></pre>

    当我们加载图表时,它会显示一个进度指示器,然后通过 CSS 过渡加载图表并逐渐淡入视图:

    <preCourier 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>

    该示例的效果就是先显示一个加载的指示器,然后加载一张图片出来,就是通常的延迟加载的效果。

    Git 仓库:http://github.com/bigskysoftware/htmx。

    来源: http://developer.51cto.com/article/714198.html

    • 推荐阅读
    • 老君山风景 人间仙境(夏日避暑老君山)

      道教圣地老君山到底怎样呢?还在等什么,快带上你的愿望出发吧!

    • 无人生还简介(无人生还简介介绍)

      以下内容希望对你有帮助!无人生还简介《无人生还》1939年发行时书名为TenLittleNiggers,后改为TenLittleIndians,是英国推理小说作家阿加莎·克里斯蒂创作的长篇小说,出版于1939年。该小说讲述了一起发生在荒岛之上的连环谋杀案。《无人生还》融合了犯罪小说之外的故事类型。如文中对不少人物,特别是布伦特小姐和维拉的心理活动有着大段描写,有些段落接近意识流小说的写法。

    • 刘海砍樵演员表(刘海砍樵简介)

      刘海砍樵演员表乔振宇饰刘海、宋佳玲饰胡秀英、TAE饰金蟾、刘涛饰大姐、边潇潇饰三姐、李蕾饰刘母、王建国饰刘振宾、徐麒雯饰杜鹃。《刘海砍樵》是由中央电视台、长沙广电艺术中心、湖南和光文化影视投资有限公司、常德市委宣传部、常德市广播电视台联合出品,该剧根据同名湖南花鼓戏改编,李惠民执导、冯海担任编剧,乔振宇、宋佳玲、徐麒雯、TAE、刘涛、边潇潇等人主演的古装神话剧。

    • 财运亨通什么意思

      出自《镜花缘》。3、成语用法:作谓语、定语;形容赚钱顺利。

    • 不系安全带扣几分啊

      应使用儿童汽车安全座椅,并系好安全带。安全带的相关规定:1.《中华人民共和国道路交通安全法实施条例》第九十条机动车驾驶人违反道路交通安全法律、法规的,处警告或者20元以上200元以下罚款。

    • 四川米豆腐怎么做好吃(四川米豆腐的做法)

      四川米豆腐怎么做好吃?四川米豆腐怎么做好吃首先把冷却好的米豆腐用刀或网状工具切成长宽高各1厘米左右的粒状,放入置有清水的盆中,这样它不易碎散。把米豆腐放入烫水中加热,2分钟即可捞出,放入配汤中,再在米豆腐中加少量的葱,使其香味更浓。最后把制作好的配菜放入米豆腐之上,如:木耳肉丝、青椒瘦肉等。吃的时候用勺稍拌一下,其味更匀、色更佳。这样就制作完成了,吃起来别有一番风味。

    • wⅰndows电脑怎么取消开机密码(Windows系统的电脑如何关闭开机时的密码)

      下面内容希望能帮助到你,我们来一起看看吧!wⅰndows电脑怎么取消开机密码打开Windows系统电脑的控制面板,点击用户账户,点击更改账户类型。输入当前密码,新密码不需要输入留空即可。修改完成后点击确定,再次开机时密码即可取消。MicrosoftWindows操作系统是美国微软公司研发的一套操作系统,它问世于1985年,起初仅仅是Microsoft-DOS模拟环境,后续的系统版本由于微软不断的更新升级,不但易用,也当前应用最广泛的操作系统。

    • 时光精灵蜜蜡(宝石之魅-时间胶囊)

      而这只蚊子,则是来自于一枚琥珀。当人们举行一些大型的庆祝活动时有燃烧琥珀的习惯,这是因为点燃的琥珀具有一种独特的松木香味。严格说来,琥珀并不属于矿物。他发现琥珀用毛皮摩擦之后会粘上一些绒毛,对于这种奇怪的现象,他认为是由于它们内部有生命力,有灵魂蕴含其中。最早,人们发现的琥珀出现在晚石炭系地层中,后来陆续在二叠纪、三叠纪和侏罗纪地层中也有发现。

    • 猜字谜答案大全三个字(好玩又好记的字谜)

      今天特意给大家带来了80条超有趣的汉字字谜,让孩子在快乐和探索中进行汉字的学习,需要的家长可以帮孩子收藏下,假期家长可以和孩子多练习一下。猜一字3、大有头,中无心,小全身。猜一字8、男宾止步,猜一个字。猜一字19、值钱不值钱,全在这两点。

    • 春江花月几人归(江春入旧年--情仍在)

      去年的时候,疫情无情的席卷了中国。“爆竹声中一岁除,春风送暖入屠苏”。但在抗击新冠肺炎时,在这危难时刻,在民族精神的感召下,因为眼里有光,心里有梦,亿万人民筑起了一道坚不可摧的“城墙”。它将全国人民的心紧紧地联系在了一起,把疫情防控常态化变成了现实。“所有随风而逝的都是属于昨天的,所有历经风雨留下来的才是面向未来的”今年,疫情虽在,但年味渐渐回归。