Javascript“evt 未定义”但可以在 Chrome 中使用
function ganttChart(gContainerID) {
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;
}
}
这在 Chrome 中工作正常,但 FF 会抛出错误:
evt 未定义
function ganttChart(gContainerID) {
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
mousemove
事件传递到this.gUpdateMousePos()
:IE 仅通过
window.event
提供当前事件。 Firefox 仅通过传递到事件处理函数的参数来提供它(您没有传递该参数,因此会出现错误)。 Chrome 和 Safari 都执行这两种操作(因此 Chrome 中不会出现错误)。You need to pass the
mousemove
event intothis.gUpdateMousePos()
: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).