Internet Explorer中的对象预期错误 [英] Object expected error in Internet Explorer

查看:95
本文介绍了Internet Explorer中的对象预期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在Firefox和Chrome中运行正常。



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


用户代理: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


当您按下链接时

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

您收到错误


消息:对象预期

行:4

字符:1

代码:0

URI:



消息:需要对象

行:24

字符:4

代码:0

URI:


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



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

  function toggleLayer(whichLayer)
{
var sub = new Array() ;
for(i = 1; i< 25; i ++)
{
sub [i] ='sub'+ i;
}
var elem,vis;
if(document.getElementById)//这是标准的工作方式
elem = document.getElementById(whichLayer);
else if(document.all)//这是旧msie版本工作的方式
elem = document.all [whichLayer];
else if(document.layers)//这是nn4工作的方式
elem = document.layers [whichLayer];
vis = elem.style;
//如果style.display值为空,我们试着在这里找出它



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';
}


解决方案

问题



Internet Explorer中的Object expected/Object required错误消息相当模糊,但是当您尝试访问未定义元素的属性时,通常会触发它。 / p>

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

  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;
}
}

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

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

相同

  eelem = document.getElementById(undefined); 

哪些返回未定义到eelem。当您访问变量eelem的属性时会触发错误,因为undefined没有属性。



解决方案



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


The code works as it should in Firefox and Chrome.

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

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

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:

There are no errors in Firefox or Chrome, just 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';
}

解决方案

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.

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 = 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.

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.

这篇关于Internet Explorer中的对象预期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆