Javascript 无法读取 null 的属性“样式”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8113729/
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-08-24 04:46:40  来源:igfitidea点击:

Cannot read property 'style' of null

javascript

提问by eric.itzhak

For some strange reason I get a weird error.

出于某种奇怪的原因,我收到了一个奇怪的错误。

I have an element (which I'd defined with PHP) that eventuly looks like this:

我有一个元素(我用 PHP 定义的)最终看起来像这样:

    <span id="l_area_1_5" style="display: inline; ">

    ...some html inputs and stuff....

    </span>

Which I am certain by Inspect Element of the Chrome.

我可以通过 Inspect Element of the Chrome 确定这一点。

Now when I try to address to it programaticly with this:

现在,当我尝试以编程方式解决它时:

document.getElementById("1_area_1_5").style.display = 'none';

I get the Cannot read property 'style' of nullerror.

我得到了Cannot read property 'style' of null错误。

I have no idea what the hell that's supposed to mean, any help?

我不知道这到底是什么意思,有什么帮助吗?

回答by Mark Byers

The error means that the element doesn't exist. It's because you have a typo. You wrote 1instead of l.

错误意味着该元素不存在。那是因为你有一个错字。你写的1而不是l.

l_area_1_5
1_area_1_5
^