这是我的代码,它使图像向右移动。我搜索了如何调用和执行函数,但我不明白它们。如何在javascript中执行javascript函数
我知道如何在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>
你想要做的第一件事就是验证你的','分号不会导致的问题。他们不是必需的。 https://www.w3schools.com/jsref/event_onclick.asp – Nol
@Nol - 他们肯定不会造成问题。最佳做法是用分号结束每条语句,而不要依赖ASI。 – Quentin
我既哑又累。花了半秒钟的时间看它,并意识到我不需要这么说。似乎OP的问题是,他们没有做他们的Init,必须将imgObj留空。 – Nol