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

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

    • 推荐阅读
    • 苏州旅游攻略景点必去(苏州旅游必去景点有哪些)

      位于苏州市东北街一百七十八号,始建于明朝正德年间。虎丘是AAAAA级景区及全国文明单位,首批十佳文明风景旅游区示范点。中午,周庄最为欢闹,游人穿梭熙熙攘攘,船儿来回摇摇荡荡,各地的游客与热情的商铺融为一体,热闹非凡,安静的古镇着实多了些欢闹的气息。狮子林为苏州四大名园之一,位于苏州市市城东北园林路。

    • 买的玉米种子是瘪的(去年买的玉米种子剩了很多)

      去年买的陈玉米种子建议不要用针对去年的陈玉米种子,大多情况下不建议再次使用,会影响到玉米后期的生长和产量情况。陈年的因为保管的问题,可能会出现很多因素影响玉米的出苗率或者后期的生长。陈玉米种子隔了一年后再种植,种子自身水分含量降低,水分降低严重的情况下,影响播种的效率和玉米的后期生长,由于活性降低,即使能出芽,也不一定能出苗。

    • 173.2亿!国庆消费火爆 国庆消费市场

      今年国庆、重阳两节叠加,全省消费市场呈现平衡较快增长态势,服装、家电、汽车等商品消费亮点突出,大众餐饮、旅行休闲、文体娱乐等主要服务消费备受青睐。根据商务部业务统一平台生存必需品监测系统显示,国庆黄金周期间,全省生存必需品市场供应充沛,价格总体平衡。除了买买买,国庆还是婚庆、团圆、会友高峰,各地亲友聚餐、婚寿宴等大众化餐饮生意兴隆。

    • 吴承恩是怎么写出的西游记(吴承恩怎么写出的西游记)

      吴承恩怎么写出的西游记诸葛长青:吴承恩写西游记诸葛长青:吴承恩怎么写出的《西游记》西游记,广泛流传西游记,作者吴承恩西游记,包含了儒释道大智慧那么,吴承恩是怎么写出的《西游记》呢?诸葛长青把自己对吴承恩写《西游记》,研究成。

    • 李逵扮演者(大家一起来看看吧)

      我们一起去了解并探讨一下这个问题吧!李逵扮演者赵小锐的李逵应该算是很多人印象当中的经典所在了,他的李逵也是很粗犷,但是这种粗犷当中却带着细腻,也是因为这个角色,他开始受到了不少的观众的关注和喜爱。其实之前的他也有出演过一些电视剧的,但是可惜的是一直都没能够真正的红起来,是李逵这个角色,让他一夜成名爆火了。

    • 汽车空间大小怎么看轴距(什么因素会影响车内空间)

      大众速腾,长度4655mm,轴距2651mm。看外观就明白了,因为宝马320i是后驱车,发动机采用纵置布局;而大众速腾是前驱车,发动机采用横置布局。而且由于发动机纵置,后驱设计,对于车内空间侵占较为严重,所以宝马320的长轴距实际上对于空间的帮助是“虚高”的。前面我们就提到了,宝马3系采用了后驱,大众速腾采用了前驱。回到我们的主题,通常来说,麦弗逊与扭力梁对于车辆空间的侵占是最小的,而多连杆和双叉臂对于车辆空间侵占是要更大的。

    • 湖南省医保局2015年工作思路与安排 湖南省医疗保障局领导班子组成人员

      督促指导各统筹地区核实提高缴费基数,强化保险费足额征收。继续加强工伤认定参与,把好工伤入口关。认真核实、积极处理群众举报问题,始终保持高压态势。加强生育医疗服务管理,规范生育津贴发放。二是启动实施工伤保险信息系统改造升级,改进工伤职工异地就医联网结算,方便工伤职工救治。三是加强财务、业务数据清理,提高数据质量;通报全省“三险”基金运行分析,指导市州加强基金运行风险管控。

    • 民国最渣四大渣男(民国著名4大渣男)

      当时很多文人在接受自由恋爱的思想时,家中已经有了父母为之安排的妻子。郁达夫一生有过三位妻子,一位同居情人。郁达夫后来还是和王映霞离婚,1940年在新加坡认识了比他小20岁的播音员李莜英,两人很快就同居了。第二任妻子佐藤富子,是个日本女人,为了和郭沫若在一起,不仅改名为“郭安娜”,还和父母断绝了关系。1937年,郭沫若抛弃妻子回国,和女明星于立群同居,两人于2年后再重庆结婚。

    • 电脑怎么连打印机教程(教会你快速学会电脑如何连接打印机的安装使用方法)

      最近很多网友都在私信给小编,小编也无法一一回复,有些问题也无法简约介绍,所以只能在头条文章内与大家共享。

    • 爱吃鸡蛋的注意了这3种鸡蛋不能买(这些鸡蛋没你想的那么好)

      营养均衡的孩子没必要补这种元素;真正缺乏硒,靠富硒蛋补,根本起不了多大作用。这类蛋再好,也别给孩子吃那就是全生或半熟的蛋,比如溏心蛋。一般溏心蛋的加热时间短,不能完全杀死细菌,生蛋液根本没有处理细菌,对于抵抗力低、易感染的宝宝来说,非常容易被细菌感染。