无法读取 null 的属性“样式” - 在 Chrome 的 Javascript 控制台中

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

Cannot read property 'style' of null - in Chrome's Javascript Console

javascriptgoogle-chromenull

提问by Livingston

I was checking the JS console out and it helped me fix a few minor issues, but I would like to know if this warning is anything to seriously worry about?

我正在检查 JS 控制台,它帮助我解决了一些小问题,但我想知道这个警告是否值得认真担心?

This is the code which gives the error:

这是给出错误的代码:

<script type="text/javascript"
src="https://www.safaviehhome.com/product_images/locations/js/jqgalscroll.js"></script>
<script src="https://www.safaviehhome.com/product_images/mainnav/stuHover.js"             type="text/javascript"></script>
<script>
function layerSetup(id,visibility){
if(document.getElementById){
this.obj = document.getElementById(id).style;
Uncaught TypeError: Cannot read property 'style' of null
Uncaught TypeError: Cannot read property 'style' of null
<!--Includes many more of the same "Cannot read property 'style' of null messages -->

this.obj.visibility = visibility;
return this.obj;}
else if(document.all){
this.obj = document.all[id].style;
this.obj.visibility = visibility;
return this.obj;}
else if(document.layers){
this.obj = document.layers[id];
this.obj.visibility = visibility;
return this.obj;}
}
function visVisible(param){
new layerSetup(param,'visible');
}

function visHidden(param){
new layerSetup(param,'hidden');
}</script>

I cannot really figure out why this is happening and whether it's something I should worry about since our Rug categories function fine. Can anybody offer any insight? I apologize for not offering any of my own explanations but I didn't write this JS a former co-worked did and it's now up to me to debug every mistake he made. It's a learning process but I'm still somewhat new to this ..

我真的无法弄清楚为什么会发生这种情况以及是否应该担心,因为我们的地毯类别功能正常。任何人都可以提供任何见解吗?我很抱歉没有提供我自己的任何解释,但我并没有像以前的同事那样写这个 JS,现在由我来调试他犯的每个错误。这是一个学习过程,但我对此还是有些陌生..

回答by Jivings

This could be happening for two reasons.

这可能有两个原因。

  1. Somewhere on the page the layerSetupfunction is being called with nullas one of the parameters.
  2. An element with the id being passed the the layerSetupfunction does not exist on the page.
  1. 在页面上的某个地方,layerSetup函数被null作为参数之一调用。
  2. 传递 id 的元素layerSetup在页面上不存在该函数。

In the Chrome dev console you can click Pause on All Exceptions, a button in the bottom left bar with a pausesymbol. This will help you to determine what the parameters are that are being passed to the function.

在 Chrome 开发者控制台中,您可以单击Pause on All Exceptions左下栏中带有暂停符号的按钮。这将帮助您确定传递给函数的参数是什么。