我正在开发一个浏览器扩展,用户可以点击弹出窗口中的一个按钮。我的代码存储在popup.js中。下面的代码应该只在您单击id=为“绿色”的按钮时执行。相反,每当您第一次单击扩展的图标时,它就会执行。
<!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"))我需要做什么才能让它只在点击id='green‘的按钮时执行?
发布于 2019-03-14 15:08:17
document.getElementById("green").addEventListener("click", function(){alert("Test")})您必须将要执行的函数传递给addEventListener。您正在做的是传递一个执行的返回
https://stackoverflow.com/questions/55155602
复制相似问题