簡體   English   中英

單擊時添加事件監聽器

[英]addEventListener on click

我有一個按鈕定義如下

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');
  ...
});

當我在應用程序 window 中單擊該按鈕時,控制台中沒有任何反應。 是否需要添加任何額外的步驟來執行此事件偵聽器?

你試過<script src="./renderer.js"></script>嗎? 您確定您的renderer.js按預期運行嗎?

澄清一下,客戶端 JavaScript 中不存在require() 。打開瀏覽器開發人員控制台,您應該會看到require()未定義。

查看MDN 腳本標簽文檔,了解有關如何包含 .js 文件的更多信息。

在您的 HTML 文件中,您在腳本標記中使用了require ,它是 Node.js 特定的 function。根據您的預期,您似乎希望將 renderer.js 鏈接到您的 HTML 文件。 因此,通過輸入<script src="renderer.js"></script>直接鏈接它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM