javascript - 点击时添加事件监听器
问题描述
我有一个按钮定义如下
在index.html
:
<body>
<section>
<button id="open-file">Open File</button>
...(series of other similar buttons)...
</section>
</body>
<script>
require('./renderer');
</script>
在renderer.js
:
const openFileButton = document.querySelector('#open-file');
...
openFileButton.addEventListener('click', () => {
console.log('You clicked the "Open File" button.');
});
在main.js
:
app.on('ready', () => {
mainWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
});
mainWindow.loadFile('app/index.html');
...
});
当我在应用程序窗口中单击该按钮时,控制台中没有任何反应。我需要添加任何额外的步骤来执行此事件侦听器吗?
解决方案
你试过<script src="./renderer.js"></script>
吗?你确定你的renderer.js
运行符合预期吗?
只是为了澄清,require()
在客户端 JavaScript 中不存在。打开你的浏览器开发者控制台,你应该会看到它require()
没有被定义。
查看MDN 脚本标签文档,了解如何包含 .js 文件的更多信息。
推荐阅读
- css - Ruby on Rails 6 how to reference images from asset pipeline for css
- botframework - AAD 身份验证 Directline & Teams,禁用安全代码
- python - 使用枕头识别数独网格中的数字
- flutter - Flutter spaceBetween 在 Card 小部件内的 Column 中不起作用
- python-3.x - 在列表对象列表中,如何将每个列表对象与所有其他列表对象进行比较?
- angular - 我可以创建多个 MsalService 实例吗
- r - 从 R 中的 for 循环创建数据框,其中包含 if-else 语句
- scala - 是否有理由使用 s String 插值而不是 f String 插值?
- c# - C# SelectCommand 属性在调用“填充”之前尚未初始化。在 WinForm 中
- jquery - 如何为要在表格中显示的数据动态分配行跨度?