Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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 为什么这个document.getElementById()函数不能工作?_Javascript_Html - Fatal编程技术网

Javascript 为什么这个document.getElementById()函数不能工作?

Javascript 为什么这个document.getElementById()函数不能工作?,javascript,html,Javascript,Html,我想使用javascript和 document.getElementByIdball.style.left=i+px 这是我在下面使用的代码。我本想使用whiletrue{}循环,但它突然出现在我身上 有人知道怎么做/解决这个问题吗 编辑 标签放置在 编辑2 我的问题是球没有移动。JavaScript是单线程的,有一些非常特殊的例外情况在这里不适用。这一个线程一次只执行一个任务。执行脚本是一项任务。重新绘制浏览器是另一个问题;回答点击是另一回事。如果您运行像while true{…}这样的无限

我想使用javascript和 document.getElementByIdball.style.left=i+px

这是我在下面使用的代码。我本想使用whiletrue{}循环,但它突然出现在我身上

有人知道怎么做/解决这个问题吗

编辑

标签放置在

编辑2


我的问题是球没有移动。

JavaScript是单线程的,有一些非常特殊的例外情况在这里不适用。这一个线程一次只执行一个任务。执行脚本是一项任务。重新绘制浏览器是另一个问题;回答点击是另一回事。如果您运行像while true{…}这样的无限任务,浏览器将再也没有时间与您交互

确保任何无限循环都被频繁的超时打破,或者更好的是,用于动画作业

constball=document.getElementById'ball'; 让位置=250; 函数滴答声{ ball.style.left=`${position}px`; 位置-=2; 如果位置>0 window.requestAnimationFrametick; } window.requestAnimationFrametick; 球{ 位置:绝对位置; 左:250px; 排名:0; }
id为ball的HTML元素是否存在?您的实际问题是什么?请张贴markupI有一个id为ball的div:你的意思可能是ballLeft-=2;而不是ballLeft=-2;我会试试这个答案!!我添加了一个简单的示例。您在哪里看到jQuery?
   <script>
        //while(true) {
        var ballLeft = 250;
        document.getElementById("ball").style.left = ballLeft;
        ballLeft =-2;
        //  }

    </script>