我在理解如何让变量在我的.onmouseup
事件中工作时遇到了一些问题。使用javascript访问变量(无jQuery).onmouseup事件
我有一个.onmousemove
事件,它定义了一个局部变量,例如自从.onmousedown
以来鼠标移动的距离。我想在执行.onmouseup
的函数中使用这些信息,但是,我无法在那里找到它。下面是代码的相关位:
document.onmousedown = function(){
var mouseStart = [event.pageX,event.pageY];
document.onmousemove = function(){
var dist = Math.sqrt(Math.pow(event.pageY-mouseStart[1],2)+Math.pow(event.pageX-mouseStart[0],2));
document.onmouseup = function() {
global_function(dist);
document.onmousemove = null;
}
}
}
我不明白为什么mouseStart
是可访问的,但我得到了得到了错误DIST是不确定的。
我还有其他的变量也需要通过,在.onmouseup
期间无法重新定义。
你为什么要在一个事件处理程序中绑定一个事件*在一个事件处理程序*内? –
他为什么不呢? – Jake
在Chrome [Fiddle]中工作正常(http://fiddle.jshell.net/Pisi2012/VQKrN/) – alexP