Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
只有";“满负荷”;JavaScript函数的形式有效吗?_Javascript_Function_Typeerror - Fatal编程技术网

只有";“满负荷”;JavaScript函数的形式有效吗?

只有";“满负荷”;JavaScript函数的形式有效吗?,javascript,function,typeerror,Javascript,Function,Typeerror,我有一个javascript函数,可以动态定位背景图像: window.onload = function() { bw = document.getElementsByClassName("BottomWrapper"); if (bw.length===0){ bw = document.getElementsByClassName("BottomWrapper2"); } pos = window.innerWidth/2 - 490.5;

我有一个javascript函数,可以动态定位背景图像:

  window.onload = function() {
    bw = document.getElementsByClassName("BottomWrapper");
    if (bw.length===0){
      bw = document.getElementsByClassName("BottomWrapper2");
    }
    pos = window.innerWidth/2 - 490.5;
    bw=Array.prototype.slice.call(bw);
    bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
  };
它是有效的。但如果我给同一个函数命名并运行它:

function position() {
  bw = document.getElementsByClassName("BottomWrapper");
  if (bw.length===0){
    bw = document.getElementsByClassName("BottomWrapper2");
  }
  pos = window.innerWidth/2 - 490.5;
  bw=Array.prototype.slice.call(bw);
  bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
};
position();
我得到一个错误:

TypeError: Cannot read property 'style' of undefined

我很困惑。为什么第一个函数起作用而第二个函数不起作用?

如果bw[0]未定义,这意味着该文档。getElementsByClassName(“BottomWrapper2”)返回未定义,这很可能意味着运行脚本时该div不在页面上。如果在标记之后加载代码,则不应发生这种情况


第一个可以工作的原因是它正在等待onload事件触发。

如果bw[0]未定义,则表示该文档返回了未定义的。getElementsByClassName(“BottomWrapper2”)很可能表示在运行脚本时div不在页面上。如果在标记之后加载代码,则不应发生这种情况


第一个可以工作的原因是它正在等待onload事件触发。

如果您这样使用该函数,它将工作

<html><head>
<script>

function position() {
  bw = document.getElementsByClassName("BottomWrapper");
  console.log(bw);
  if (bw.length===0){
    bw = document.getElementsByClassName("BottomWrapper2");
  }
  pos = window.innerWidth/2 - 490.5;
  bw=Array.prototype.slice.call(bw);
  bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
  console.log(bw[0]);
};
   </script>
<body onload="position()">
<div class="BottomWrapper2"></div>
</body></head></html>

函数位置(){
bw=document.getElementsByClassName(“BottomWrapper”);
console.log(bw);
如果(bw.length==0){
bw=document.getElementsByClassName(“BottomWrapper2”);
}
位置=窗内宽度/2-490.5;
bw=Array.prototype.slice.call(bw);
bw[0]。style.backgroundPosition=“中心0px,+pos+”px 0px”;
console.log(bw[0]);
};

前面的函数会导致错误,因为在加载主体之前调用该函数

如果像这样使用该函数,它将正常工作

<html><head>
<script>

function position() {
  bw = document.getElementsByClassName("BottomWrapper");
  console.log(bw);
  if (bw.length===0){
    bw = document.getElementsByClassName("BottomWrapper2");
  }
  pos = window.innerWidth/2 - 490.5;
  bw=Array.prototype.slice.call(bw);
  bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
  console.log(bw[0]);
};
   </script>
<body onload="position()">
<div class="BottomWrapper2"></div>
</body></head></html>

函数位置(){
bw=document.getElementsByClassName(“BottomWrapper”);
console.log(bw);
如果(bw.length==0){
bw=document.getElementsByClassName(“BottomWrapper2”);
}
位置=窗内宽度/2-490.5;
bw=Array.prototype.slice.call(bw);
bw[0]。style.backgroundPosition=“中心0px,+pos+”px 0px”;
console.log(bw[0]);
};

前面的函数会导致错误,因为在加载主体之前调用了函数

您是否在询问
window.onload
的作用?window.onload=position()@差不多了。删除
()
@Craig通过删除“坏”函数并仅按名称使用“好”函数来简化问题;e、 g.
window.onload=position
position()
相比如何。提取
位置的定义
消除了“相同”功能的重复和“差异”的潜在来源。一旦这样做了,就可以有把握地认为执行上下文才是重要的,或者如果其他方面出了问题。(另外,我建议使用局部变量来避免污染。)Oops。我的意思是,如果没有
()
,你会问
window.onload
做什么?window.onload=position()@差不多了。删除
()
@Craig通过删除“坏”函数并仅按名称使用“好”函数来简化问题;e、 g.
window.onload=position
position()
相比如何。提取
位置的定义
消除了“相同”功能的重复和“差异”的潜在来源。一旦这样做了,就可以有把握地认为执行上下文才是重要的,或者如果其他方面出了问题。(另外,我建议使用局部变量来避免污染。)Oops。我的意思是,如果没有
()
“swell”,我会在页面加载后从javascript控制台运行“position()”函数,得到相同的结果。当我将该函数附加到window.resize事件时,该函数也可以工作。另外,我在div下面的页脚中有代码……所以我不认为这是问题所在。@Craig通过删除“bad”函数并仅按名称使用“good”函数来简化问题;e、 g.
window.onload=position
position()
相比如何。提取
位置的定义
消除了“相同”功能的重复和“差异”的潜在来源。一旦这样做了,就可以有把握地认为执行上下文才是重要的,或者如果其他方面出了问题。(同时,我建议使用局部变量来避免污染。)我的坏。。。我在页脚中有代码,但事实证明我在一个单独的文件中也有相同的代码,该文件包含在页眉中。。。很抱歉出现了误报,感谢您的回复!在页面加载后,我从javascript控制台运行了'position()'函数,得到了相同的结果。当我将该函数附加到window.resize事件时,该函数也可以工作。另外,我在div下面的页脚中有代码……所以我不认为这是问题所在。@Craig通过删除“bad”函数并仅按名称使用“good”函数来简化问题;e、 g.
window.onload=position
position()
相比如何。提取
位置的定义
消除了“相同”功能的重复和“差异”的潜在来源。一旦这样做了,就可以有把握地认为执行上下文才是重要的,或者如果其他方面出了问题。(同时,我建议使用局部变量来避免污染。)我的坏。。。我在页脚中有代码,但事实证明我在一个单独的文件中也有相同的代码,该文件包含在页眉中。。。很抱歉出现了误报,感谢您的回复!我的错。。。我在页脚中有代码,但事实证明我在一个单独的文件中也有相同的代码,该文件包含在页眉中。。。很抱歉出现了误报,感谢您的回复!我的错。。。我在页脚中有代码,但事实证明我在一个单独的文件中也有相同的代码,该文件包含在页眉中。。。很抱歉出现了误报,感谢您的回复!