Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
IE中的Javascript对象必需错误_Javascript_Object_Animation - Fatal编程技术网

IE中的Javascript对象必需错误

IE中的Javascript对象必需错误,javascript,object,animation,Javascript,Object,Animation,我正在用javascript创建多个行星对象来处理动画 动画对每个星球都很好,但我在IE 6/7中遇到错误,说“第15行需要对象2” 代码: 我在mac上,没有任何IE可以尝试。如果您这样更改代码,是否会出现相同的错误: function init() { var earthObj = null; var mercObj = null; var jupiObj = null; var animate; mercObj = document.getElementById('m

我正在用javascript创建多个行星对象来处理动画

动画对每个星球都很好,但我在IE 6/7中遇到错误,说“第15行需要对象2”

代码:


我在mac上,没有任何IE可以尝试。如果您这样更改代码,是否会出现相同的错误:

function init() {
  var earthObj = null;
  var mercObj = null;
  var jupiObj = null;
  var animate;

  mercObj = document.getElementById('mercury');
  earthObj = document.getElementById('earth');
  jupiObj = document.getElementById('jupiter');

  mercObj.style.position= 'relative';  
  mercObj.style.left = '54px'; 
  mercObj.style.visibility = 'hidden';


  !earhtObj && alert("There is no element with id 'earth'");
  earthObj.style.left = '80px'; 
  earthObj.style.top = '300px';
  earthObj.style.position= 'relative'; 
}
我来到accros post后想,这个错误是否与IE6/7错误有关,当某个全局变量与dom对象同名时,会触发IE6/7错误


我还移动了
earthObj.style.position='relative'到块的末尾,并期望错误出现在earthObj.style.left='80px'

我在mac上,没有任何IE可供尝试。如果您这样更改代码,是否会出现相同的错误:

function init() {
  var earthObj = null;
  var mercObj = null;
  var jupiObj = null;
  var animate;

  mercObj = document.getElementById('mercury');
  earthObj = document.getElementById('earth');
  jupiObj = document.getElementById('jupiter');

  mercObj.style.position= 'relative';  
  mercObj.style.left = '54px'; 
  mercObj.style.visibility = 'hidden';


  !earhtObj && alert("There is no element with id 'earth'");
  earthObj.style.left = '80px'; 
  earthObj.style.top = '300px';
  earthObj.style.position= 'relative'; 
}
我来到accros post后想,这个错误是否与IE6/7错误有关,当某个全局变量与dom对象同名时,会触发IE6/7错误


我还移动了
earthObj.style.position='relative'到块的末尾,并期望错误出现在earthObj.style.left='80px'

在尝试调用对象之前,测试它是否存在

earthObj = document.getElementById('earth');
if(!earthObj) {
  alert("Could not find Earth");
  return;
}

在尝试调用对象之前,请测试它是否存在

earthObj = document.getElementById('earth');
if(!earthObj) {
  alert("Could not find Earth");
  return;
}

我发现在IE中,如果脚本在定义/生成的HTML元素之后,该函数就可以工作

i、 e.将脚本放在HTML文档的末尾,而不是开头,或者使用jquery的ready函数:

$(function() {
   mercObj = document.getElementById('mercury');
});

我发现在IE中,如果脚本在定义/生成的HTML元素之后,该函数就可以工作

i、 e.将脚本放在HTML文档的末尾,而不是开头,或者使用jquery的ready函数:

$(function() {
   mercObj = document.getElementById('mercury');
});

可能您缺少earth?向我们显示标记,以及
标记的位置。可能您缺少earth?向我们显示标记,以及
标记的位置。如果不在
样式中。左
行,则错误将在符号29上的
样式.top
行中弹出。无论如何:)如果不在
style.left
行中,错误将在符号29的
style.top
行中弹出。无论如何:)