這是我的代碼,它使圖像向右移動。我搜索瞭如何調用和執行函數,但我不明白它們。如何在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