IE中的Javascript对象必需错误
我正在用javascript创建多个行星对象来处理动画 动画对每个星球都很好,但我在IE 6/7中遇到错误,说“第15行需要对象2” 代码: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
我在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
行中弹出。无论如何:)