javascript - 在 Firefox 中显示 'ReferenceError: event is not defined' 的 D3 事件

标签 javascript firefox d3.js dom-events

我在我的 d3 map 中使用鼠标滚轮缩放。正在使用以下代码。

.on("wheel.zoom", function() {
    var currScale = projection41039.scale();
    var newScale = currScale - 1 * event.deltaY;
    if (newScale >= 150) {
        var currTranslate = projection41039.translate();
        var coords = projection41039.invert([event.offsetX, event.offsetY]);
        projection41039.scale(newScale);
        var newPos = projection41039(coords);
        projection41039.translate([currTranslate[0] + (event.offsetX - newPos[0]),
            currTranslate[1] + (event.offsetY - newPos[1])
        ]);
        updateContents();
    }
})

这对 Chrome 来说工作正常,但在 Firefox 中会抛出错误:

ReferenceError: event is not defined

最佳答案

此处的问题是 Chrome 遵循 Window.event 的 Internet Explorer 功能,而 Firefox 没有:

enter image description here

例如,给定一个按钮或任何其他元素,以下代码片段可以在 Chrome 上运行,但不能在 Firefox 上运行:

d3.select("button").on("click", function() {
  console.log(event)
})

在这里试试:https://jsfiddle.net/b9h4ntn0/ (我通常使用 Stack Snippets,但在该特定示例中该代码段会卡住)

解决方案

使用d3.event。这样,您就不会依赖 Window.event:

d3.select("button").on("click", function() {
  console.log(d3.event)
})

以下代码适用于 Chrome 和 Firefox:https://jsfiddle.net/qj787zmj/

关于javascript - 在 Firefox 中显示 'ReferenceError: event is not defined' 的 D3 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227127/

相关文章:

javascript - 如何获得可用的无符号 32 位整数?

Javascript 点击 href 链接

javascript - Selenium - 无响应的脚本错误 (Firefox)

javascript - 取消嵌套和减少javascript中的数据结构

javascript - 当轴标签映射为刻度域的一部分时,如何在 d3 中修改堆积条形图的轴标签

javascript - 无法分配给对象的只读属性

javascript - 未捕获的类型错误 : loadOptions is not a function at Async. loadOptions

javascript - ui.draggable 显示确认是否被取消。

html - 如何修复 Firefox 中的 span/div 问题(在 IE 中按需要显示)并调整 Chrome 中的错位?

javascript - 使用 JavaScript/D3 在图表上设置间距条