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

相关推荐
ghfdgbg1 分钟前
12. AOP(记录日志)
前端
我命由我123452 分钟前
微信小程序 - 页面返回并传递数据(使用事件通道、操作页面栈)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
一水鉴天2 分钟前
整体设计 定稿 备忘录仪表盘方案 之1 初稿之8 V5版本的主程序 之2: 自动化导航 + 定制化服务 + 个性化智能体(豆包助手)
前端·人工智能·架构
vortex513 分钟前
【Web开发】从WSGI到Servlet再到Spring Boot
前端·spring boot·servlet
于谦17 分钟前
git提交信息也能自动格式化了?committier快速体验
前端·javascript·代码规范
小高00719 分钟前
React 避坑指南:彻底搞定不必要的重新渲染
前端·javascript·react.js
浩浩酱24 分钟前
【TS】any的问题及与unknown的区别
前端·typescript
dagouaofei24 分钟前
手术室护理年终PPT怎么做?
前端·python·html·powerpoint
技术爬爬虾25 分钟前
为什么React的漏洞能攻破服务器?Next.js与RSC入门基础
前端·数据库·安全
JS_GGbond26 分钟前
浏览器三大核心API:LocalStorage、Fetch API、History API详解
前端·javascript