简体   繁体   English

如何对 Electron 应用程序中的按钮单击做出反应

[英]How to react to button click in Electron app

Please could I ask for help with the following:请问我可以就以下问题寻求帮助:

I have cloned the Electron starter app like so:我已经克隆了 Electron 入门应用程序,如下所示:

git clone https://github.com/electron/electron-quick-start git 克隆https://github.com/electron/electron-quick-start

And have it running.并让它运行。

I have added one line:我添加了一行:

<button id="doitButton" onclick="alert('t')">DoIt</button>

Here it is in the context of the main.js file:这是在 main.js 文件的上下文中:

    <!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'; style-src 'self' 'unsafe-inline'">
    <link href="./styles.css" rel="stylesheet">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <button id="doitButton" onclick="alert('t')">DoIt</button>
    We are using Node.js <span id="node-version"></span>,
    Chromium <span id="chrome-version"></span>,
    and Electron <span id="electron-version"></span>.

    <!-- You can also require other files to run in this process -->
    <script src="./renderer.js"></script>
  </body>
</html>

I am working in VSCode.我在 VSCode 工作。 I am sure to save the file and then I run "npm start".我确定会保存文件,然后运行“npm start”。

I see the button, but no alert when I click it.我看到该按钮,但单击它时没有警报。 What have I done wrong?我做错了什么?

Thanks for any help.谢谢你的帮助。

Take a look at electron dialogs here: https://www.electronjs.org/docs/v14-xy/api/dialog在此处查看 electron 对话框: https://www.electronjs.org/docs/v14-xy/api/dialog

I have found a solution.我找到了解决方案。

If I comment out the line:如果我注释掉这一行:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">

Then I see the alert appearing.然后我看到警报出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM