简体   繁体   English

未捕获的类型错误:无法读取 null 的属性(读取“addEventListener”)。 该怎么办?

[英]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种可能性:

  1. Your selector for startButton is incorrect.您的 startButton 选择器不正确。 So you have to adjust your selector.所以你必须调整你的选择器。

  2. 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.

相关问题 未捕获的类型错误:无法读取 javaScript 中的 null 的属性(读取“addEventListener”) - Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') in javaScript 未捕获的类型错误:无法读取 null 的属性(读取“addEventListener”)Chrome 扩展 - Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') Chrome Extension 未捕获的类型错误:无法读取 null 的属性(读取“addEventListener”)在(索引):72 - Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at (index):72 未捕获的类型错误:无法读取 null 读取 addeventlistener 的属性 - Uncaught TypeError:cannot read properties of null reading addeventlistener 类型错误:无法读取 null 的属性(读取“addEventListener”) - TypeError: Cannot read properties of null (reading 'addEventListener') 未捕获的类型错误:无法读取 null 的属性(正在读取“addEventListener”)我该如何解决? - Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') how can I fix it? 我在基本 JS 按钮中收到“Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')”错误 - I get the "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')" error in a basic JS button script.js:23 Uncaught TypeError: 无法读取 null 的属性(读取“addEventListener”) - script.js:23 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') 如何修复 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') "loadeddata" in mobile audio api - How to fix Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') "loadeddata" in mobile audio api 未捕获的类型错误:无法读取未定义的属性(读取“addEventListener”) - Uncaught TypeError: Cannot read properties of undefined (reading 'addEventListener')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM