有人能告诉我这段代码有什么问题吗?我无法让它发挥作用。 我正在尝试使此按钮可单击,但它一直给出错误: 未捕获的SyntaxError:无效或意外的令牌
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")});
答案 0 :(得分:1)
试一试
document.getElementById('demo').addEventListener('click', function(){
alert('clicked')
})
<button type="button" id='demo'>Click Me!</button>
document.getElementById('demo').addEventListener('click', function(){
alert('clicked')
})
<p id="demo">Click me</p>
答案 1 :(得分:1)
您应该将script.js文件的声明移到您引用的实际DOM元素下面。
所以你的HTML应该是这样的:
<body>
<button id="myBtn"> A </button>
<script src="../JS/Javascript.js" type="text/javascript">
</script>
<h1> Psychic Game </h1>
答案 2 :(得分:0)
你最好先学习js。 它应该是:
document.getElementById("myBtn").addEventListener("click", function(){console.log("computerChoice")});