简体   繁体   English

在 React.js 中禁用右键单击?

[英]Disable right click in React.js?

I have the following element that I would like to prevent from being downloaded by disabling the right click.我有以下元素,我想通过禁用右键单击来防止被下载。

<iframe src={TEST + "#toolbar=0"} width="100%" height="800px" 
onMouseDown={(e)=>e.preventDefault()} onContextMenu={(e)=>e.preventDefault()}/> 

Unfortunately, when I right-click, it still brings up the context menu.不幸的是,当我右键单击时,它仍然会弹出上下文菜单。 Any idea why?知道为什么吗?

Use the contextmenu event inside componentDidMount() method of your component.在组件的componentDidMount()方法中使用contextmenu事件。

For example:例如:

componentDidMount() {
  document.addEventListener('contextmenu', (e) => {
    e.preventDefault();
  });
};

This will prevent the context menu to be shown.这将阻止显示上下文菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM