douji9184 2014-07-09 12:16
浏览 876
已采纳

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

I have some d3.js element plotted, eg:

 // 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?

  • 写回答

2条回答 默认 最新

  • dongpixi2648 2014-07-09 18:13
    关注

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

     // 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
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?