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

相关推荐
云水一下7 小时前
从零开始!VMware安装Fedora Workstation 44桌面系统完整教程
前端
小码哥_常8 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
前端
killerbasd8 小时前
还是迷茫 5.3
前端·react.js·前端框架
不会敲代码19 小时前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
kyriewen9 小时前
奥特曼借GPT-5.5干杯,而你的Copilot正按Token收钱
前端·github·openai
AC赳赳老秦10 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen10 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
千寻girling10 小时前
《 Git 详细教程 》
前端·后端·面试
之歆11 小时前
DAY08_CSS浮动与行内块布局实战指南(下)
前端·css
yqcoder12 小时前
CSS Position 全解析:5 种定位模式详解
前端·css