开发者

Is it possible to disable the context menu on a canvas element?

开发者 https://www.devze.com 2023-03-30 15:58 出处:网络
I\'m using jqPlot to create a graph inside a div tag.jqPlot used the canvas element to render the graph.I\'m trying to add some interactivity to the graph, i.e. zooming, where the user can zoom in and

I'm using jqPlot to create a graph inside a div tag. jqPlot used the canvas element to render the graph. I'm trying to add some interactivity to the graph, i.e. zooming, where the user can zoom in and out using the using the left and right mouse-click, respectively. I want to disable the context menu when the user right-clicks on the graph. I've tried several approaches, including...

Adding the the oncontextmenu attribute to the parent div containing the graph canvas:

<div id="myGraph" style="width: 908px; height: 600px; " class="jqplot-target" align="center" oncontextmenu="return false"></di开发者_开发知识库v>

Dynamically applying the oncontextmenu param to all the children after jqPlot renders the graph:

//Code to render the graph
//...
$("#myGraph").children().attr("oncontextmenu", "return false");

Using jQuery's approach instead of regular JS:

$("#myGraph").children().bind("contextmenu",function(e){
        return false;
    });

And using a custom plug-in from this great site and applying it's disable function as follows:

$("#plot_testEventHistogram").children().disableContextMenu();

But none of these approaches seem to work, I still get a context menu when I right-click on the graph. It's not a showstopper, but is very annoying and would be nice if it can be effectively disabled


canvas.oncontextmenu = function() {
    return false;
}

http://jsfiddle.net/9ZWv2/

Works in chrome

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号