初学者提问:为什么这个代码会报错?点击按钮没有反应?
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>井字棋</title>
</head>
<body style = "background-color: #1f1f1f;">
<h1 id = "tictactoeTitle1" style = "text-align: center; font-size: 224px; color: #dfdfdf; display: block; margin-top: 64px;">井字棋</h1>
<button id = "tictactoeButton1" onclick = "startgameClick()" style = "text-align: center; font-size: 80px; border-radius: 16px; padding: 64px 144px; background-color: #dfdfdf; border: 0px; font-weight: bold; color: #3f3f3f; display: block; margin: 0 auto; margin-top: 256px;">开始</button>
</body>
<script>
startgameClick = function () {
var tictactoeTitle1Set = document.getElementById(tictactoeTitle1);
tictactoeTitle1Set.style.display = "none";
}
</script>
</html>
点击按钮之后就会这样报错:
Uncaught TypeError TypeError: Cannot read properties of null (reading 'style')
at startgameClick (e:\Programs HTML\Test\Test1-Tictactoe\tictactoe.html:16:28)
at onclick (e:\Programs HTML\Test\Test1-Tictactoe\tictactoe.html:11:280)
script放在了最后,为什么还是出问题?