【发布时间】:2017-05-16 14:17:43
【问题描述】:
这是使图像向右移动的代码。我已经搜索了如何调用和执行函数,但我不明白。
我知道如何在 HTML 中执行它,但不是在 javascript 中。我想在我一直加载页面时执行它。
<input type="button" value="Start" onclick="moveRight();" />
我已经看到了这段代码,但我无法让它与我的代码一起使用。
window["functionName"](arguments);
例如我应该在括号中写的内容。
<script>
var imgObj = null;
var animate ;
function init(){
imgObj = document.getElementById('myImage');
imgObj.style.position= 'relative';
imgObj.style.left = '0px';
}
function moveRight(){
imgObj.style.left = parseInt(imgObj.style.left) + 11 + 'px';
animate = setTimeout(moveRight,22); // call moveRight in 20msec
}
<body>
<form>
<img id="myImage" src="myimage.jpg" />
<p>Click the buttons below to handle animation</p>
<input type="button" value="Start" onclick="moveRight();" />
<input type="button" value="Start" onclick="moveLeft();" />
<input type="button" value="Stop" onclick="stop();" />
</form>
【问题讨论】:
-
您要做的第一件事是验证您的
;分号不会导致问题。它们不是必需的。 w3schools.com/jsref/event_onclick.asp -
@Nol — 他们肯定不会造成问题。最佳做法是用分号结束每条语句,而不是依赖 ASI。
-
我既笨又累。又看了半秒,意识到我没必要这么说。似乎 OP 的问题是他们没有做他们的 Init,这必须让 imgObj 为空。
标签: javascript function call