简体   繁体   English

如何防止鼠标右键单击onMouseDown()反应js?

[英]How to prevent right mouse button click onMouseDown() react js?

I am using onMouseDown and onMouseUp for better control.我正在使用onMouseDownonMouseUp进行更好的控制。 I already have onContextMenu but how I can prevent the right button click on onMouseDown and onMouseUp .I only want the left button to be clicked.我已经有onContextMenu但是如何防止右键单击onMouseDownonMouseUp 。我只希望单击左键。

because currently 2 events triggering at the same time.因为目前有 2 个事件同时触发。 My context menu and other function open at the same time.我的上下文菜单和其他 function 同时打开。

You can check if e.button === 2 or not.您可以检查e.button === 2与否。 When you right click then the value of e.button is 2 .当您右键单击时, e.button的值为2

Though i'm not sure about it's browser support.虽然我不确定它是否支持浏览器。

You can check this example.你可以检查这个例子。

 const {useState} = React; const App = () => { const [value, setValue] = useState('left or right click'); const handleMouseDown = (e) => { if(e.button === 2) return; console.log('handleMouseDown'); } const handleMouseUp = (e) => { if(e.button === 2) return; console.log('handleMouseUp'); } const handleContextMenu = (e) => { console.log('handleContextMenu'); } return ( <div className="test" onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} onContextMenu={handleContextMenu} >{value}</div> ); }; // Render it ReactDOM.render( <App/>, document.getElementById("react") );
 .test { width: 100vw; height: 100vh; text-align: center; font-size: 20px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> <div id="react"></div>

onMouseDown={(event) => {
 if (event.button === 0) { ***write some code***}
}}

event.button === 0 left , event.button === 1 middle , event.button === 2 right , event.button === 0 left , event.button === 1 middle , event.button === 2 right ,

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

相关问题 如何防止在 React JS 中单击按钮时发生布局偏移 - how to prevent layout shift on button click in react js 为什么我的 React js 按钮 onMouseDown 不起作用? - Why isn't my React js button onMouseDown not working? 单击菜单按钮时反应JS,整个页面向右移动 - React JS when click menu button, entire page shift right 如何使用 onKeyPress (React JS) 激活右键? - How activate the right button with onKeyPress (React JS)? React js-单击链接后如何防止页面重新加载。 现在整个页面都在点击链接时刷新 - React js-How to prevent page reload once click on the link. Right now the whole page is getting refresh on clicking the link React js - 在鼠标的新选项卡中右键单击打开它打开多个选项卡时,使用 window.open() - React js - On right click open in new tab of mouse it opens multiple tab when, used window.open() 如何在leaflet反应js中获取当前鼠标点击的坐标 - How to get coordinates of current mouse click in leaflet react js 如何在反应js中单击按钮时显示警报? - how to show alert on button click in react js? 如何在 React js 中单击按钮时调用组件? - How to call component on button click in React js? 如何在反应JS中单击按钮打开表单 - How to open form on button click in react JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM