【发布时间】:2018-05-30 10:11:00
【问题描述】:
我正在开发一个应用程序并尝试主要使用 jquery,我已经尝试了所有在线示例,但我似乎无法找到我的错误所在。请有人可以查看我的代码并告诉我我做错了什么.. 这是我的代码..
const {app, document, BrowserWindow, Menu} = require('electron')
const path = require('path')
const url = require('url')
//define jquery..
window.$ = require('jquery')(window);
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
function createWindow () {
// Create the browser window.
win = new BrowserWindow({
name:"AtikuDesk",
width: 396,
height: 356,
frame:false,
transparent: true,
toolbar: false
});
// and load the index.html of the app.
win.loadFile('assets/index.html')
// Open the DevTools.
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
menuarr = [
{
label:'Home',
submenu:[
{label:'About App'},
{label:'view statistics'},
{label:'get help'}
]
},
{
label:'Exit',
click(){
app.quit()
}
}
];
var menu = Menu.buildFromTemplate(menuarr);
Menu.setApplicationMenu(menu);
function clsApp(e){
app.quit();
}
$('#clsbtn').bind('click', clsApp);
最后一个函数不起作用,它不起作用,它显示一个窗口未定义的错误,当我尝试使用此代码时.. 没有 Jquery,
document.querySelector('#clsbtn').addEventListener('click', clsApp)
它一直告诉我文档未定义。我无法在网上找到合适的解决方案,如果有人帮我解决这个问题,我会很高兴。
我更喜欢从单独的 javascript 文件中处理我的所有事件和操作,而不是在 html 页面上的内联脚本标签中处理。
谢谢。
【问题讨论】:
-
看起来代码在
document完成加载之前正在运行。你是如何在你的 html 文件中加载脚本的? -
很好的观察,我应该如何加载它。因为我在这里使用电子,如果它是纯 javascript,我会知道该怎么做......
-
再次查看您的代码,您似乎正在尝试在 Main 进程中使用 jQuery,这是错误的方法。您应该在渲染器进程中使用它。
assets/index.html我认为这是您的渲染器 html 页面。您应该在 that 页面中加载 jQuery。是定义#clsbtn的那个页面吗? -
并查看here 了解如何在电子中加载 jquery