javascript “未捕获的类型错误:无法读取属性”JS 错误是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33276423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:18:21  来源:igfitidea点击:

What does the "Uncaught TypeError: Cannot read property" JS error mean?

javascript

提问by Jobvdweerd

Can someone explain to me what these errors mean?

有人可以向我解释这些错误是什么意思吗?

I need the answers for a school project.

我需要学校项目的答案。

Error 1 "Uncaught TypeError: Cannot read property 'getBoundingClientRect' of null"
Error 2 "Uncaught TypeError: Cannot read property 'dom' of null"

回答by JimmyRare

It means you are trying to use a property on something that is null. Make sure you have an object to operate on before trying to use the property. I.e getBoundingClientRectexpects a dom element.

这意味着您正在尝试对null 的内容使用属性。在尝试使用该属性之前,请确保您有一个要操作的对象。即getBoundingClientRect期望一个 dom 元素。

One way to select a dom element is by id:

选择 dom 元素的一种方法是通过 id:

var element = document.getElementById('myElement');

var element = document.getElementById('myElement');

More about selecting dom elements here.

更多关于在此处选择 dom 元素的信息。