electron框架使用button点击事件

2,469 阅读6分钟

问题描述:

在使用electron时,index.html中写了一个

<button onclick="click">点击</button>

但是提示错误信息:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

翻译以后是这样的:

拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:“脚本-src 'self'”。启用内联执行需要‘unsafe inline’关键字、一个散列(‘sha256-…’)或者一个nonce(‘nonce-…’)。

解决办法:

js写click的方法不止一种,但是chorme不允许onclick时间,所以改写为:

<button id="button">点击</button>

js文件中:

document.getElementById('button').addEventListener('click',function(){
    console.log('aaa')
})

问题解决。