我有一张关于事件onload的设置图片:
function hideMe() {
document.getElementById("navy1").style.display = "none";
}
我还有另一个在屏幕上设置图片的功能:
function setUpPicture() {
subRunnerOBJ = new Object();
subRunnerOBJ.topPos = 100;
subRunnerOBJ.leftPos = 0;
subRunnerOBJ.velX = 400;
subRunnerOBJ.velY = 0;
subRunnerOBJ.score = 0;
snImgObj = document.getElementById("navy1");
//在这一点上我得到了消息“Microsoft JScript运行时错误:无法获取属性'样式'的值:对象为空或未定义”
snImgObj.style.left = subRunnerOBJ.leftPos + "px";
snImgObj.style.top = subRunnerOBJ.topPos + "px";
snImgObj.style.position = "absolute";
snImgObj.style.display = "show";
startMovePicture();
任何想法为什么这么开心?
//this one will set the the pictue on the right side of the screen
function setUpPicture() {
subRunnerOBJ = new Object();
subRunnerOBJ.topPos = 100;
subRunnerOBJ.leftPos = 0;
subRunnerOBJ.velX = 400;
subRunnerOBJ.velY = 0;
subRunnerOBJ.score = 0;
//document.getElementById("navy1").style.display = "show";
snImgObj = document.getElementById("navy1");
snImgObj.style.left = subRunnerOBJ.leftPos + "px";
snImgObj.style.top = subRunnerOBJ.topPos + "px";
snImgObj.style.position = "absolute";
snImgObj.style.display = "show";
//once we place the location of the sub , we will Call to new function that will move it
startMovePicture();
}
function startMovePicture() {
dt = 50; // in miliseconds
h = setInterval("moveObj(subRunnerOBJ)", dt);
}
function moveObj(someObj) {
counter = 0;
while (counter < 3000) {
subRunnerOBJ.leftPos = subRunnerOBJ.leftPos + subRunnerOBJ.velX * dt / 1000;
subRunnerOBJ.topPos = subRunnerOBJ.topPos + subRunnerOBJ.velY * dt / 1000;
snImgObj.style.left = subRunnerOBJ.leftPos + "px";
snImgObj.style.top = subRunnerOBJ.topPos + "px";
counter = counter + 50;
if (counter == 3000) {
stopRunning()
}
}
}
function stopRunning() {
clearInterval(h);
hideMe();
}
//this function will hide the pucture on liad , and once the loop with the Pictue Running will end
//once loading the page we will not see the Picture
function hideMe() {
document.getElementById("navy1").style.display = "none";
}
}
答案 0 :(得分:1)
无法获取属性“style”的值:object为null或 未定义“
这意味着您尝试使用对象的属性,而该对象实际上是null
或undefined
值。在您的情况下,snImgObj
为null
。
这是因为以下代码无法找到ID为“navy1”的元素
snImgObj = document.getElementById("navy1"); // snImgObj is NULL in your case
原因可能有很多:
如果您打算使用getElementById
创建元素,则应使用以下方法:
var element = document.createElement('div'); // creates a dangling DOM node
someOtherElement.appendChild(element); // appends it to another element
旧版本的Internet Explorer似乎不允许以这种方式创建图像,但您可以read this answer查看如何以跨浏览器方式完成图像。
同时强>
CSS display
属性不理解“show”。您可能希望将其设置为“阻止”。有关显示属性,请参阅documentation。
snImgObj.style.display = "block"; // this is valid
在声明变量时始终使用var
!您正在污染全局范围。
setInterval
,accepts functions。答案 1 :(得分:0)
问题是snImgObj
未设置为setUpPicture()
中的元素。仔细检查您的HTML。