简体   繁体   English

事件监听器在扩展图标点击时触发,而不是弹出按钮

[英]Event listener triggers on extension icon click, not popup button

I'm developing a browser extension where the user can click on one of many buttons in the popup.我正在开发一个浏览器扩展,用户可以在其中单击弹出窗口中的许多按钮之一。 I have my code stored in popup.js.我的代码存储在 popup.js 中。 The following code is supposed to execute only when you click the button with id="green".以下代码应该仅在您单击 id="green" 的按钮时执行。 Instead, it executes whenever you click the extension's icon for the first time.相反,它会在您第一次单击扩展程序图标时执行。

 <!DOCTYPE html> <html> <head> <style> button { height: 20px; width: 100px; outline: none; } </style> </head> <body> <button id="oolong">Oolong</button> <button id="green">Green</button> <button id="purerh">PurErh</button> <button id="Black">Black</button> <span id="audio"></span> <script src="popup.js"></script> </body> </html>

document.getElementById("green").addEventListener("click", alert("Test"))

What do I need to do to make it only execute on the click of the button with id='green'?我需要做什么才能让它只在点击带有 id='green' 的按钮时执行?

document.getElementById("green").addEventListener("click", function(){alert("Test")})

You have pass to addEventListener a function to execute.您必须向addEventListener传递一个要执行的函数。 What you are doing is passing the return of a execution您正在做的是传递执行的返回

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

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