在 React 中禁用 Firefox 的右键单击上下文菜单

Mar*_*man 1 javascript firefox contextmenu handsontable reactjs

我正在尝试禁用右键单击特定div. 在渲染中我做

<div onContextMenu={(e) => {
    console.log(e); 
    e.preventDefault(); 
    e.stopPropagation(); 
    return false
}}>
Run Code Online (Sandbox Code Playgroud)

它确实打印,所以我知道它是附加的,但它仍然会在 firefox ESR 60.8.0 中弹出,即使它在 chrome 中被阻止。

原因:我有一个手持台,我向其中添加了自定义上下文菜单,并且在 Firefox 中,本机上下文菜单呈现在我的顶部。一旦我弄清楚如何在 Firefox 中的任何位置阻止上下文菜单,我会将其应用于自定义渲染器中的 handsontable

编辑:开始赏金是因为其他黑客都没有为我工作,这是一个关于罕见版本的 Firefox 的非常晦涩的案例

Mar*_*aov 5

您需要在捕获阶段关闭菜单打开。

document.addEventListener("contextmenu", function(e) {
    if(e.target.id === "YOUR ELEMENT ID") { // identify your element here. You can use e.target.id, or e.target.className, e.target.classList etc...
        e.preventDefault();
        e.stopPropagation();
    }
}, true) // true means you are executing your function during capture phase
Run Code Online (Sandbox Code Playgroud)

您可以在 componentDidMount 期间或在 React 代码之外设置此代码。