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

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

    • 推荐阅读
    • 荷塘月色简笔画彩图(荷塘的场景图简笔画)

      今日份简笔画荷塘月色.感恩日记1.,我来为整理几张简单漂亮的荷塘月色简笔画彩图?以下简笔画图片总有一款是你喜欢的,希望对你有帮助来看看吧!荷塘月色简笔画彩图今日份简笔画荷塘月色.感恩日记1.荷塘月色好看的儿童画图片儿童简笔画大全荷塘月色简笔画儿童画

    • 直硬头发软化前后效果图(头发软化前后效果图)

      可以使头发变软,变柔顺,变贴服,且价格也很便宜,普通的美发沙龙价格为50到80元左右,软化比较自然。头发软化后几天可以洗刚做完软化2至3天不要洗头,刚做完软化不要用力拉头发,会有损发质和效果。软化也是伤头发的,不过比不停地做一次性夹头发而言小很多,如果是短发做软化还是不错的。如果想让头发蓬蓬的,最好不要全头做软化,甚至不建议做软化。具体情况,建议咨询理发师。用药水要用好一点的,对头发伤害才不会很大。

    • 赞美运动员的话(赞美运动员的话有什么)

      年轻的我们自信飞扬,青春的气息如同出生的朝阳,蓬勃的力量如同阳光的挥洒此时此刻,跑道便是我们精彩的舞台,声声加油便是我们最高的奖项论何成功,谈何荣辱,心中的信念只有一个:拼搏,我来为大家科普一下关于赞美运动员的话?赞美运动员的话年轻的我们自信飞扬,青春的气息如同出生的朝阳,蓬勃的力量如同阳光的挥洒。所有的努力都是为了迎接这一刹那,所有的拼搏都是为了这一声令下。

    • 长安uni-k车主反映这款车怎么样(新车长安UNI-K登场)

      据长安汽车最新消息,中大型SUVUNI-K官图曝光,这是长安UNI系列的第二款车型。新车将搭载蓝鲸系列2.0T发动机并匹配8AT变速箱,将于广州车展首发亮相。新车亮点1.采用了全新的“V”型面设计和无边界格栅。新车概况新车前脸依然采用无边界设计并融入了V型面概念,不同于UNI-T,UNI-K的大灯位置设计在了最上方。车尾方面采用了时下流行的贯穿式尾灯设计,与UNI-T的V型后导流造型不同,UNI-K采用了新的航天器式造型,立式尾灯十分显眼。

    • 渡劫经典语录(关于渡劫的语录精选)

      情到深处人孤独,爱至穷时尽沧桑堕落的天使啊,你无知的游走着。我将于茫茫人海中访我唯一灵魂之伴侣;得之,我幸;不得,我命。玲珑骰子安红豆,入骨相思君知否。于千万人之中遇见你所遇见的人,于千万年时间无涯的荒野里,没有早一步,也没有晚一步,刚巧赶上了。生命是一朵千瓣莲花,我拒绝了绽放的同时,我也拒绝了枯萎和零落。就算哭泣也要皱眉优雅,就算失败也要转身潇洒。之后我也学会了阳奉阴违,发生了什么与我再无所谓。

    • 孤城闭什么时候上映(谁是主演)

      以下内容大家不妨参考一二希望能帮到您!孤城闭什么时候上映《孤城闭》将于2020年起在湖南卫视上映播出。该剧主要由王凯、江疏影、任敏、杨玏、边程、叶祖新、喻恩泰、王楚然、刘钧、孙坚等主演。《孤城闭》改编自米兰lady同名小说,以北宋为背景,在风起云涌的朝堂之事与剪不断理还乱的儿女情长之间,还原了一个复杂而真实的宋仁宗。

    • 大众朗逸所有灯图解(认识汽车灯图解)

      大众朗逸所有灯图解作为新手,汽车灯光就是一道难题,下面我们一起通过图解来认识一下汽车各种灯光吧。双闪灯的作用是当车辆发生意外情况后,引起其他车辆警惕,防止发生追尾事故。当踩下制动踏板后,制动灯立即亮起,并发出红色灯光,提醒后方车辆。倒车灯是白色,作用是为了照亮车尾的路面,减少倒车时盲区,另外也是对后方的提醒。

    • 雪里红的腌制方法(做雪里红腌菜的步骤)

      下面更多详细答案一起来看看吧!雪里红的腌制方法雪里红摘干净,根部用刀劈开,正一层反一层放入盆中,取盐均匀地洒在雪里红上,腌制1-2天。烧开水放凉,加盐,搅拌均匀,盐水倒入雪里红中泡制一天。泡好的雪里红捆成一小捆放入密封罐,倒入泡雪里红的盐水,盖好密封罐,即吃即取。

    • 郑州婚纱照推荐哪家好(郑州拍婚纱照团购)

      中国红喜嫁秀爆朋友圈的婚纱照中式婚纱照新中式婚纱照婚纱照风格高级感婚纱照婚纱照秀禾服的中式嫁衣,是完美诠释了东方女性温婉古典美。让人完全移不开目光~每一个女孩子都应该拥有这样华丽的喜嫁风太精致完全属于中式婚纱照的浪漫感~

    • 胎梦最准的位置(从胎梦看看你腹中的孩子给你暗示了吗)

      估计生完孩子和正在孕期的妈妈都会经历过这种事情,就是我们会经常做梦,而且会梦见一些动物植物什么的,这在老人眼里属于“胎梦”。你梦见的什么会预示着即将出生的宝宝是男孩还是女孩。你的胎梦准不准,来看看一下别人的胎梦。哈哈,看来有些胎梦还是挺准的,或许都是巧合吧,总之,宝宝来了就是我们的命中注定。