获取元素的问题

时间:2012-06-18 23:00:52

标签: javascript

我有一张关于事件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";  
    }



    }

2 个答案:

答案 0 :(得分:1)

  

无法获取属性“style”的值:object为null或   未定义“

这意味着您尝试使用对象的属性,而该对象实际上是nullundefined值。在您的情况下,snImgObjnull

这是因为以下代码无法找到ID为“navy1”的元素

snImgObj = document.getElementById("navy1"); // snImgObj is NULL in your case

原因可能有很多:

  1. 您的元素根本不存在。
  2. 您忘记设置ID或拼写错误的声明。
  3. 在试图通过id获取之后创建元素
  4. 您的元素存在,但尚未完成加载。
  5. ......等。
  6. 如果您打算使用getElementById 创建元素,则应使用以下方法:

    var element = document.createElement('div'); // creates a dangling DOM node
    someOtherElement.appendChild(element); // appends it to another element
    

    旧版本的Internet Explorer似乎不允许以这种方式创建图像,但您可以read this answer查看如何以跨浏览器方式完成图像。

    同时

    1. CSS display属性不理解“show”。您可能希望将其设置为“阻止”。有关显示属性,请参阅documentation

      snImgObj.style.display = "block"; // this is valid
      
    2. 在声明变量时始终使用var您正在污染全局范围。

    3. 请勿将字符串传递给setIntervalaccepts functions
    4. 使用JSLint验证您的代码。它会告诉你大多数问题在哪里。这是值得的!

答案 1 :(得分:0)

问题是snImgObj未设置为setUpPicture()中的元素。仔细检查您的HTML。