我有一个按钮,当我单击它时希望它执行一个功能,但是由于某种原因,它根本不起作用。我认为将按钮链接到js文件的方式可能有问题。这是我的HTML:
<!DOCTYPE html>
<html lang = "en-us"></html>
<head>
<script src = "spotifybot.js" charset = "utf-8"></script>
<title>Spotify Login</title>
</head>
<body>
<button id = "login" style = "width: 100px;height: 100px">Login To Spotify</button>
</body>
</html>
这是我的js文件:
const loginButton = document.getElementById("login").addEventListener('click', loginToSpotify());
const request = require("request");
function loginToSpotify()
{
console.log("Here");
}
如果您认为可能是问题所在,则两个文件都位于同一文件夹中。
编辑:检查浏览器控制台后(我以为我可以检查我的Visual Studio控制台,但显然不能),我发现错误与“ require”关键字有关
未定义require,并且您不想在设置侦听器时执行功能。
const loginButton = document.getElementById("login").addEventListener('click', loginToSpotify);
//const request = require("request");
function loginToSpotify()
{
console.log("Here");
}
<head>
<script src = "spotifybot.js" charset = "utf-8"></script>
<title>Spotify Login</title>
</head>
<body>
<button id = "login" style = "width: 100px;height: 100px">Login To Spotify</button>
</body>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句