Electron 引入 jQuery(11)

到目前为止,我使用了浏览器API来操作前端。这不是一个编写前端代码好的方法。因此在接下来的几篇文章,我将使用不同的前端框架开发。

从备受喜爱的经典jQuery开始。

安装jQuery

复制代码
npm install jquery

我们可以从以前的项目中复制除了app.js脚本之外的所有代码。

加载jQuery

xml 复制代码
<script src="./node_modules/jquery/dist/jquery.js"></script>

app.js

现在我们可以使用jQuery改写DOM操作。

js 复制代码
function appendInput(command) {
  let e = $(
    `<div class='input-line'>
      <span class='prompt'>$</span>
      <span class='input'></span>
    </div>`)
  e.find('.input').text(command)
  $("#history").append(e)
}

function appendOutput(output) {
  let e = $(`<div class='output'></div>`)
  e.text(output)
  $("#history").append(e)
}

$("form").on("submit", function(e) {
  e.preventDefault()
  let command = $("input").val()
  let output = window.api.runCommand(command)
  appendInput(command)
  appendOutput(output)
  $("input").val("")
  $("input")[0].scrollIntoView()
})

安全

现在,使用字符串 <span class='input'>${command}</span> 是很有吸引力的,但这是不安全的。如果命令包含像<>特殊字符,应用程序将表现不正确。

还有其他方法可以更有表现力,更安全。例如模板字符串和各种模板库,如 handlebars

代码

地址:github.com/fengjutian/...

原文:dev.to/taw/electro...

相关推荐
时光足迹5 分钟前
极光推送全攻略(上):被iOS证书折磨了三天,我写了一份前端也能看懂的避坑指南
前端·ios·uni-app
DyLatte8 分钟前
AI 时代,最危险的不是被替代,而是努力不沉淀
前端·后端·程序员
mCell34 分钟前
【锐评】桌面端技术营销:别拿跑分当工程判断
前端·rust·electron
柒和远方1 小时前
从一次工程审查看 AI 学习产品的边界兜底:RAG 资料链路一致性实战
前端·后端·架构
疯狂的魔鬼1 小时前
一个"懂分寸"的文本省略组件是怎样炼成的
前端·vue.js·设计
angerdream1 小时前
手把手编写儿童手机远程监控App之vue3 AI Gent
前端
李明卫杭州1 小时前
CSS BFC 完全指南:从原理到实战,彻底搞懂这个"结界"
前端
Momo__1 小时前
MDN MCP Server——Mozilla 把 Web 文档接进 AI Agent,从此 LLM 不再瞎编 API
前端·ai编程·mcp
妙码生花1 小时前
现代前端的极致性能 icon 加载方案(死磕成功版)
前端·vue.js·typescript
掘金者阿豪2 小时前
把业务数据变成共享仪表盘:Metabase可视化与远程访问实践
前端·后端