【问题标题】:My app doesn't close on button, electron.js我的应用程序没有在按钮上关闭,electron.js
【发布时间】:2021-04-13 13:54:53
【问题描述】:

我有这个 electron.js 应用程序,我希望在单击类为 closer 的文本时关闭应用程序。这是我的代码:

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title>Hello World!</title>
    <link rel="stylesheet" href="../style.css">
  </head>
  <body>

  
    <div class="content">
      <h1>Hello</h1>
      <h1 class="closer">UUUU</h1>
    </div>
 
    <script src="./renderer.js"></script>
    <script src="./closer.js"></script>
  </body>
</html>

main.js初始化电子

const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 600,
    backgroundColor: "red",
    frame: false,
    webPreferences: {
      nodeIntegration: true,
     
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')


  // mainWindow.webContents.openDevTools()
}


app.whenReady().then(() => {
  createWindow()
  
  app.on('activate', function () {
    
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})


app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

closer.js

const { app } = require('electron');
const babi = function(){
    const bubu = document.querySelector('.closer');
    function bubub(){
        bubu.innerHTML = "aganim";
        app.quit();
    }
    bubu.addEventListener('click', bubub);
}
babi();

我的问题是单击按钮不起作用。我该怎么办?

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:
    [...bubu].forEach(bubu => 
      bubu.addEventListener("click", bubuClose()) 
    );
    
     function bubuClose() {
       bubu.innerHTML = "aganim";
       window.close();
     };
    

    我认为你可以用这样的循环来做到这一点。

    【讨论】:

    • 这个方法行得通,谢谢,但是,将来我可以用下面的方法运行其他节点js代码吗?喜欢运行爬虫或机器人
    • 这是一个循环。将永远有效。如果它有效,请不要忘记将其标记为已解决。
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多