使用 d3.js,我遇到了一个奇怪的问题。以下代码工作正常:
var zoomHandler = d3.behavior.zoom()
.scaleExtent([min, max])
.on("zoom", function() {
// do something;
}).on("zoomend", function() {
// do something;
});
但是,我还希望我的 zoomHandler 停用另一个事件,所以我尝试像这样扩展代码:
var zoomHandler = d3.behavior.zoom()
.scaleExtent([min, max])
.on("zoom", function() {
// do something;
}).on("zoomend", function() {
// do something;
}).on("otherEvent", null);
但是现在,在运行时,我得到一个异常
Uncaught TypeError: Cannot read property 'on' of undefined
为什么我只能添加两个监听器而不能添加三个?
最佳答案
您的问题不在于事件的数量,而是事件类型“otherEvent”(或您在代码中使用的任何类型)。缩放行为仅支持 zoom events :
The following types are supported:
- zoomstart - at the start of a zoom gesture (e.g., touchstart).
- zoom - when the view changes (e.g., touchmove).
- zoomend - at the end of the current zoom gesture (e.g., touchend).
关于javascript - d3 : only two "on" possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262061/