最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use both "d3.event.preventDefault" and "d3.event.which" - Stack Overflow

programmeradmin3浏览0评论

This code works perfect — except in Internet Explorer 11. The deleteNode(d) only calls if the mousedown handle is mented out.

circle.enter().append('circle')
  .on('contextmenu', function (d) {
    deleteNode(d);
  })
  .on('mousedown', function (d) {
    setNode(d);
  });

That's why I try to catch right click with mousedown, but the context menu still appears.

circle.enter().append('circle')
  .on('mousedown', function (d) {
    d3.event.preventDefault();
    if (d3.event.which == 3) {
      deleteNode(d);
    }
    setNode(d);
  });

How to fix not showing up the context menu or catching both contextmenu and mousedown events?

This code works perfect — except in Internet Explorer 11. The deleteNode(d) only calls if the mousedown handle is mented out.

circle.enter().append('circle')
  .on('contextmenu', function (d) {
    deleteNode(d);
  })
  .on('mousedown', function (d) {
    setNode(d);
  });

That's why I try to catch right click with mousedown, but the context menu still appears.

circle.enter().append('circle')
  .on('mousedown', function (d) {
    d3.event.preventDefault();
    if (d3.event.which == 3) {
      deleteNode(d);
    }
    setNode(d);
  });

How to fix not showing up the context menu or catching both contextmenu and mousedown events?

Share Improve this question edited Apr 12, 2018 at 23:16 diralik 7,2464 gold badges34 silver badges54 bronze badges asked May 11, 2015 at 14:53 AgataAgata 821 gold badge1 silver badge6 bronze badges 2
  • 1 check the answer to this question, might be your cause...stackoverflow./questions/14839440/jquery-mousedown-vs-click – hubson bropa Commented May 11, 2015 at 15:02
  • Thank you for your input! I don't have another "click" handler (just, touchend, mouseover, mouseend, dbltap) and it doesn't work with click. – Agata Commented May 11, 2015 at 15:07
Add a ment  | 

1 Answer 1

Reset to default 7

You are close to your own solution to the problem. All that is needed is already there, you just need to rearrange it a bit:

circle.enter().append("circle")
    .on("contextmenu", function(d) {
        d3.event.preventDefault();
    })
    .on("mousedown", function (d) {
        if (d3.event.which == 3) {
            deleteNode(d);  // <-- d probably needs to be replaced
        } else {
            setNode(d);    // <-- d probably needs to be replaced
        }
    });

This works for me in IE11 as well as in FF and Chrome.

As an aside, please note that d refers to the datum bound to the node, not to the node itself. Within the event listener this will refer to the current node.

var svg = d3.select("svg");                
svg.append("circle")
    .attr({
        "cx": 100,
        "cy": 100,
        "r": 100
    })
    .on("contextmenu", function(e) {
        d3.event.preventDefault();
    })
    .on("mousedown", function (e) {
        if (d3.event.which == 3) {
          //console.log("deleteNode(d)");
        } else {
          //console.log("setNode(d)");
        }
    });
<script src="https://cdnjs.cloudflare./ajax/libs/d3/3.4.11/d3.min.js"></script>

<svg width="400" height="400">
</svg>

发布评论

评论列表(0)

  1. 暂无评论