窗口未定义,文档未定义 - 电子

时间:2018-05-30 10:11:00

标签: jquery window electron

我正在开发一个应用程序,并尝试使用大多数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页面上的内联脚本标记中处理。

感谢。

1 个答案:

答案 0 :(得分:1)

从主JS文件中删除此代码。然后在index.html:

<script>window.$ = window.jQuery = require('jquery');</script>
<script src="index.js"></script>

和index.js:

function clsApp(e){
   app.quit();
}
$('#clsbtn').bind('click', clsApp);