gpt4 book ai didi

javascript - JS 无法使事件监听器工作

转载 作者:行者123 更新时间:2023-11-30 15:28:31 24 4
gpt4 key购买 nike

有人能告诉我这段代码有什么问题吗?我无法让它工作。我试图让这个按钮可以点击,但它一直报错:未捕获的语法错误:无效或意外的标记

HTML:

        <!DOCTYPE html>
<html lang="en">
<head>
<link rel="Stylesheet" href="../CSS/Reset.css" type="text/css">
<link rel="Stylesheet" href="../CSS/style.css" type="text/css">
<meta charset="UTF-8">
<title>Psychic Game</title>

</head>
<body>
<script src="../JS/Javascript.js" type="text/javascript">
</script>
<button id="myBtn"> A </button>
<h1> Psychic Game </h1>

<h3> Guess what letter im thinking of </h3>

<h3> Wins: </h3>

<h3> Losses: </h3>

<h3> Guesses Left: </h3>

<h3> Your Guesses so far: </h3>

JS:

var computerChoice = letterBank[Math.floor(Math.random() * letterBank.length)];
function log() {
console.log(computerChoice);
}
//document.getElementById("myBtn").addEventListener("click", function(){console.log("computerChoice")});
document.getElementById("myBtn").addEventListener("click", function(){console.log("computerChoice")});

最佳答案

您应该将 script.js 文件的声明移到您所引用的实际 DOM 元素下方。

所以你的 HTML 应该是这样的:

<body>
<button id="myBtn"> A </button>
<script src="../JS/Javascript.js" type="text/javascript">
</script>
<h1> Psychic Game </h1>

关于javascript - JS 无法使事件监听器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42593168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com