是否可以检测到路径/链接上的左键单击。我想知道如何在 mousedown 内检测到它。我看到这个链接how-to-distinguish-between-left-and-right-mouse-click-with-jquery但可能是可能的解决方案。
正如您在代码中看到的。我将 true 应用于上下文菜单中的变量 isLeftClick,这样当我单击 mousedown 时,isLeftClick 的值为 false。但问题是 mousedown 会先执行。
isLeftClick = false;
path.enter().append('svg:path')
.attr('class', 'link')
.classed('selected', function(d) { return d === selected_link;
})
.on('mousedown', function(d) {
// detect if it is right or left click
if(isLeftClick == true){
//if left click do something
isDraggingLink = true;
}
restart();
}).on('contextmenu', function(d){
isLeftClick = true;
// Open a context menu for link/path
console.log("link contextmenu");
});
最佳答案
在“mousedown”回调中,您可以访问 d3.event 变量。它包含对 DOM 事件的引用,该事件包含有关触发回调的事件的完整信息。
参见https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#d3_event
和https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
这应该有效:
var leftButtonPressed = (d3.event.button === 0);
关于javascript - 我们如何检测路径上的左键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412168/