【问题标题】:why is my eventlistener not working in electron为什么我的事件监听器不能在电子中工作
【发布时间】:2020-03-06 19:04:04
【问题描述】:

我正在尝试学习 Electron js。 我的 index.html 中有一个非常简单的 HTML

  <!doctyp html> 

   ... all the other normal declarations ...

  <button class="alert">alert</button>
  <script>
    const btn = document.querySelector('.alert');
    btn.addEventListener('click', () => { alert('alerting');}) 
  </script>

由于某种原因,这在 Electron 中不起作用。当我在任何其他网络浏览器中运行此代码时,它运行良好。怎么回事?

谢谢

【问题讨论】:

  • stackoverflow.com/questions/38858879/… - Electron 不支持alert,因为它会阻塞执行流程。使用 Electrons 内置对话框来实现类似的非阻塞效果。
  • @somethinghere 这最多是事实的一半。只需阅读所有文本并评论链接的答案。我们可以调用window.alert,因为电子为此提供了一个实现。只是 window.prompt 不起作用,我们需要自定义实现,例如 electron-dialog
  • 当我刚刚在我的脚本标签中写了``` ```。它显示了一个警报,但是当使用事件侦听器时它不起作用

标签: events electron


【解决方案1】:

我设法解决了这个问题。 在我的元标签中,我有以下标签。

<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; connect-src *" />

我改成:

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

现在它可以工作了。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2017-07-24
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2016-07-30
    相关资源
    最近更新 更多