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

相关推荐
岁岁岁平安4 分钟前
Vue3学习(组合式API——reactive()和ref()函数详解)
前端·javascript·vue.js·学习·vue3·reactive·ref
肠胃炎6 分钟前
React事件机制
前端·javascript·react.js
CUIYD_198914 分钟前
javascript —— ! 和 !! 的区别与作用
前端·javascript·vue.js
帅帅哥的兜兜2 小时前
next.js实现项目搭建
前端·react.js·next.js
筱歌儿2 小时前
css 左右布局
前端·css
GISer_Jing2 小时前
编译原理AST&以Babel为例进行解读、Webpack中自定义loader与plugin
前端·webpack·node.js
GISer_Jing2 小时前
Webpack中Compiler详解以及自定义loader和plugin详解
前端·webpack·node.js
浩~~2 小时前
CSS常用选择器
前端·css
于慨2 小时前
uniapp+vite+cli模板引入tailwindcss
前端·uni-app
yunvwugua__3 小时前
Python训练营打卡 Day26
前端·javascript·python