Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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
Javascript 获取元素的问题_Javascript - Fatal编程技术网

Javascript 获取元素的问题

Javascript 获取元素的问题,javascript,Javascript,我有一张图片正在事件加载上设置: function hideMe() { document.getElementById("navy1").style.display = "none"; } 我还有另外一个功能,可以在屏幕上设置图片: function setUpPicture() { subRunnerOBJ = new Object(); subRunnerOBJ.topPos = 100; subRu

我有一张图片正在事件加载上设置:

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运行时错误:无法获取属性“style”的值:对象为null或未定义”

知道为什么会这样吗

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



    }
//此选项将设置屏幕右侧的图像
函数setUpPicture(){
subRunnerOBJ=新对象();
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=子runnerobj.leftPos+“px”;
snImgObj.style.top=subRunnerOBJ.topPos+“px”;
snImgObj.style.position=“绝对”;
snImgObj.style.display=“show”;
//一旦我们放置了子节点的位置,我们将调用新函数来移动它
startMovePicture();
}
函数startMovePicture(){
dt=50;//以毫秒为单位
h=设定间隔(“移动对象(subRunnerOBJ)”,dt;
}
函数moveObj(someObj){
计数器=0;
同时(计数器<3000){
subRunnerOBJ.leftPos=subRunnerOBJ.leftPos+subRunnerOBJ.velX*dt/1000;
subRunnerOBJ.topPos=subRunnerOBJ.topPos+subRunnerOBJ.velY*dt/1000;
snImgObj.style.left=子runnerobj.leftPos+“px”;
snImgObj.style.top=subRunnerOBJ.topPos+“px”;
计数器=计数器+50;
如果(计数器==3000){
停止运行()
}
}
}
函数stopRunning(){
净距(h);
hideMe();
}
//此函数将隐藏liad上的折叠,一旦Pictue运行的循环结束
//一旦加载页面,我们将看不到图片
函数hideMe(){
document.getElementById(“navy1”).style.display=“无”;
}
}

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

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

这意味着您试图使用对象的属性,而该对象实际上是
null
未定义的值。在您的情况下
snImgObj
null

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

原因可能有很多:

  • 您的元素根本不存在
  • 您忘记设置id或将id声明拼写错误
  • 您在尝试按id获取元素后创建了该元素
  • 您的元素存在,但尚未完成加载
  • ……等等
  • 如果要使用
    getElementById
    创建元素,则应使用以下方法:

    var element = document.createElement('div'); // creates a dangling DOM node
    someOtherElement.appendChild(element); // appends it to another element
    
    旧版本的Internet Explorer似乎不允许以这种方式创建图像,但您可以看看如何以跨浏览器的方式创建图像

    同时

  • CSS
    display
    属性不理解“show”。您可能希望将其设置为“block”。有关display属性,请参阅

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

  • 不要将字符串传递给
    setInterval
    ,否则会导致错误
  • 用于验证您的代码。它将告诉您大多数问题在哪里。这是值得的

  • 这是还是?给我们看完整的代码,这里有些东西是不可能的。
    var element = document.createElement('div'); // creates a dangling DOM node
    someOtherElement.appendChild(element); // appends it to another element
    
    snImgObj.style.display = "block"; // this is valid