为什么这个动画功能不起作用?

时间:2013-08-28 13:25:28

标签: javascript function alert

所以这段代码有效:

<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>

唯一的区别是功能,哪里出错?

PS:我需要它作为一个功能

2 个答案:

答案 0 :(得分:-1)

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

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

答案 1 :(得分:-1)

尝试全局放置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);