所以这段代码有效:
<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:我需要它作为一个功能
答案 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);