javascript Internet Explorer 中的对象预期错误

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

Object expected error in Internet Explorer

javascript

提问by Stephen Woodward

The code works as it should in Firefox and Chrome.

该代码在 Firefox 和 Chrome 中正常工作。

When the page loads in Internet Explorer, you get the error

在 Internet Explorer 中加载页面时,您收到错误

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB6.5; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; eSobiSubscriber 2.0.4.16; FDM; InfoPath.3) Timestamp: Wed, 18 Aug 2010 13:48:54 UTC

用户代理:Mozilla/4.0(兼容;MSIE 8.0;Windows NT 6.1;WOW64;Trident/4.0;GTB6.5;SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729;Media Center PC 6.0;eSobiSubscriber 2.0.4.16;FDM;InfoPath.3) 时间戳:2010 年 8 月 18 日星期三 13:48:54 UTC

and when you press a link

当你按下一个链接

<a href="javascript:toggleLayer('sub< ? echo $l; ?>');"><? echo $alp[$l]; ?></a> 

you get the error

你得到错误

Message: Object expected
Line: 4
Char: 1
Code: 0
URI:

Message: Object required
Line: 24
Char: 4
Code: 0
URI:

消息:预期对象
行:4
字符:1
代码:0
URI:

消息:需要对象
行:24
字符:4
代码:0
URI:

There are no errors in Firefox or Chrome, just IE.

在 Firefox 或 Chrome 中没有错误,只有 IE。

Any help would be appreciated. Here is my code:

任何帮助,将不胜感激。这是我的代码:

function toggleLayer( whichLayer )
{
  var sub=new Array();
  for (i=1;i<25;i++)
  {
  sub[i] = 'sub'+i;
 }
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here



  for (i=1;i<26;i++)
 {
   eelem = document.getElementById( sub[i] );
   vvis = eelem.style;
   if(eelem==elem){
    vvis.display = "block";
    } else {
    vvis.display = "none";
    }
 }

  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

回答by HoLyVieR

The Problem

问题

The "Object expected"/"Object required" error message in Internet Explorer is rather vague, but it's usually triggered when you are trying to access an attribute of an element which is undefined.

Internet Explorer 中的“预期对象”/“需要对象”错误消息相当模糊,但通常在您尝试访问未定义元素的属性时触发。

In your case, the undefined element you are probably trying to access is most probably located here :

在您的情况下,您可能尝试访问的未定义元素很可能位于此处:

for (i=1;i<26;i++)
 {
   eelem = document.getElementById( sub[i] ); // <---- UNDEFINED
   vvis = eelem.style; // <----- ERROR
   if(eelem==elem){
    vvis.display = "block";
    } else {
    vvis.display = "none";
    }
 }

The element "eelem" is undefined on the last iteration of the array, because you have previously only initialized the array "sub" with the index 1 to 24. In the last iteration your are trying to access the index 25. So in the last iteration here's what happen :

元素“eelem”在数组的最后一次迭代中未定义,因为您之前仅使用索引1 到 24初始化了数组“sub” 。在最后一次迭代中,您试图访问索引25。所以在最后一次迭代中发生了什么:

eelem = document.getElementById( sub[i] );

Is the same as

是相同的

eelem = document.getElementById( undefined );

Which return undefined to eelem. The error is triggered when you are accessing an attribute of the variable eelem, because undefined does not have attribute.

哪个返回 undefined 给 elelem。当您访问变量 eelem 的属性时会触发该错误,因为 undefined 没有属性。

The Solution

解决方案

To prevent this from happening you should either initialize the array with the index 1 to 25 or in your second loop, only loop the element 1 to 24.

为了防止这种情况发生,您应该使用索引 1 到 25 初始化数组,或者在第二个循环中,只循环元素 1 到 24。