[英]Uncaught TypeError: Cannot read properties of null (reading 'addEventListener'). What to do?
I'm designing a quiz app, but while console logging for a click event, this statement showed up on the console.我正在设计一个测验应用程序,但是在控制台记录单击事件时,此语句出现在控制台上。 Code -
代码 -
startButton.addEventListener("click", function startGame() {
console.log("started");
});
what should i do to show the statement on my console???我应该怎么做才能在我的控制台上显示声明???
If you get this error it means that your start button is null.如果您收到此错误,则表示您的开始按钮为空。 2 possibilities:
2种可能性:
Your selector for startButton is incorrect.您的 startButton 选择器不正确。 So you have to adjust your selector.
所以你必须调整你的选择器。
Your selector is correct but your button is not in the DOM yet.您的选择器是正确的,但您的按钮尚未在 DOM 中。 So you have a problem with timing.
所以你有时间问题。 Try to add the eventlistener at the end of your code
尝试在代码末尾添加事件监听器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.