Javascript 'evt is undefined' but works in Chrome
function ganttChart(gCon开发者_运维技巧tainerID) {
this.gContainer = document.getElementById(gContainerID);
this.gCurrentMouseX;
this.gCurrentMouseY;
this.gAttatchMove = function(self) {
self.gContainer.onmousemove = function() {
if (self.gIsDraggingBar) {
self.gUpdateMousePos();
self.gBarBeingDragged.style.left = (self.gBarStartX - (self.gMouseStartX - self.gCurrentMouseX)) + "px";
}
};
self.gContainer.onmouseup = function() {
self.gIsDraggingBar = false;
};
}
var self = this;
this.gAttatchMove(self);
// Update mouse coords
this.gUpdateMousePos = function(evt) {
if (window.event) {
this.gCurrentMouseX = event.x;
this.gCurrentMouseY = event.y;
}
else {
this.gCurrentMouseX = evt.x;
this.gCurrentMouseY = evt.y;
}
}
This works fine in Chrome, but FF throws the error:
evt is undefined
You need to pass the mousemove
event into this.gUpdateMousePos()
:
self.gContainer.onmousemove = function(evt) {
if (self.gIsDraggingBar) {
self.gUpdateMousePos(evt);
// And the rest follows here
}
};
IE only supplies the current event via window.event
. Firefox only supplies it via a parameter passed into the event handler function (which you were not passing on, hence the error). Chrome and Safari do both (hence no error in Chrome).
精彩评论