簡體   English   中英

為什么此動畫功能不起作用?

[英]Why doesn't this animation function work?

所以這段代碼有效:

<script>
function animate() {
    var x = 0;
    var goUp = document.getElementById('rotationUp');
    var comeDown = document.getElementById('rotationDown');

    goUp.onmouseover = alert(x);
}
</script>

這不是:

<script>
function animate() {
    var x = 0;
    var goUp = document.getElementById('rotationUp');
    var comeDown = document.getElementById('rotationDown');

    goUp.onmouseover = function() {
    alert(x);
    }
}
</script>

唯一的區別是function ,我在哪里出錯?

PS:我需要它作為功能

嘗試

function animate() {
    var x = 0;
    var goUp = document.getElementById('rotationUp');
    var comeDown = document.getElementById('rotationDown');

    goUp.onmouseover = function() {
        alert(x);
    }
}

嘗試將x變量全局放置:

<script>
var x = 0;

function animate() {
    var goUp = document.getElementById('rotationUp');
    var comeDown = document.getElementById('rotationDown');

    goUp.onmouseover = function() {
        alert(x);
    }
}
</script>

如果不起作用,請嘗試window.x

        alert(window.x);

this.x

        alert(this.x);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM