简体   繁体   中英

Why doesn't this animation function work?

so this code works:

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

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

and this doesn't:

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

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

The only difference is the function , where am i going wrong?

PS: i need it to work as a function

try

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

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

Try to place your x variable globally:

<script>
var x = 0;

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

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

If it doesn't work try window.x :

        alert(window.x);

Or this.x :

        alert(this.x);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM