javascript 右键单击 d3.js 元素:如何防止出现浏览器上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24653471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:10:17  来源:igfitidea点击:

right-click on a d3.js element: how to prevent browser context menu to appear

javascriptd3.js

提问by Askar Ibragimov

I have some d3.js element plotted, eg:

我绘制了一些 d3.js 元素,例如:

 // draw rectangle 
  svg.selectAll(".rect").append("rect")
        .attr("y", 10)
        .attr("x", 10)
        .attr("height", 5)
        .attr("width", 5)
        .on("contextmenu", function (d, i) {  
    // react on right-clicking
 });

and it works fine but also opens browser's context menu. How I can prevent that from happening?

它工作正常,但也会打开浏览器的上下文菜单。我怎样才能防止这种情况发生?

回答by Brian

Add d3.event.preventDefault();to your function.

添加d3.event.preventDefault();到您的功能中。

 // draw rectangle 
  svg.selectAll(".rect").append("rect")
        .attr("y", 10)
        .attr("x", 10)
        .attr("height", 5)
        .attr("width", 5)
        .on("contextmenu", function (d, i) {
            d3.event.preventDefault();
           // react on right-clicking
        });

回答by saikiran.vsk

I understood and giving below code for you, check it and ask if you need any thing more...

我理解并为您提供以下代码,检查它并询问您是否需要更多...

document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(event)
{
  if(event.button==2)
   {
     alert(status);
     return false;    
   }
}