右击是Javascript事件吗?如果是,我该如何使用它?
当前回答
For track right click
window.oncontextmenu = () =>{
console.log("Right click")
}
仅适用于右键单击
其他回答
在JQuery中,您可以使用以下代码检测它:
$('.target').on('contextmenu', function (evt) { evt.preventDefault(); }); $('.target').mouseup(function (evt) { if (evt.which === 3) { // right-click $(this).css("background-color","blue"); $(this).text("RIGHT"); } else if (evt.which === 1) { $(this).css("background-color","red"); $(this).text("LEFT"); } }); .target { display: inline-block; height: 100px; width: 100px; background: gray; text-align: center; color: white; font-size: 25px; vertical-align: middle; margin: 25px; } .container { width: 100%; height: 140px; background: #AAA; vertical-align: middle; text-align: center; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="target" id="target">Click</div> <div class="target" id="target">Right</div> <div class="target" id="target">Click me!</div> </div>
如果你想调用函数,而右击事件意味着我们可以使用以下
<html lang="en" oncontextmenu="func(); return false;">
</html>
<script>
function func(){
alert("Yes");
}
</script>
这是我的工作
if (evt.xa.which == 3)
{
alert("Right mouse clicked");
}
您可能想尝试以下属性:
按钮- (caniuse); Which - (caniuse)(弃用)。
function onMouseDown(e)
{
if (e.which === 1 || e.button === 0)
{
console.log('Left mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 2 || e.button === 1)
{
console.log('Middle mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 3 || e.button === 2)
{
console.log('Right mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 4 || e.button === 3)
{
console.log('Backward mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 5 || e.button === 4)
{
console.log('Forward mouse button at ' + e.clientX + 'x' + e.clientY);
}
}
window.addEventListener("mousedown", onMouseDown);
document.addEventListener("contextmenu", function(e)
{
e.preventDefault();
});
相关:演示
操作系统
On Windows and Linux there are modifier keys Alt, Shift and Ctrl. On Mac there’s one more: Cmd, corresponding to the property metaKey... Even if we’d like to force Mac users to Ctrl+click – that’s kind of difficult. The problem is: a left-click with Ctrl is interpreted as a right-click on MacOS, and it generates the contextmenu event, not click like Windows/Linux. So if we want users of all operating systems to feel comfortable, then together with ctrlKey we should check metaKey. For JS-code it means that we should check if (event.ctrlKey || event.metaKey)...
在本章中,我们将详细介绍鼠标事件及其属性……
来源:https://amazon.com/dp/B07DZWLPG9
是的,虽然w3c说右键点击可以通过点击事件检测到,onClick不是通过在通常的浏览器中单击右键触发的。
事实上,右键只触发onMouseDown onMouseUp和onContextMenu。
因此,您可以将“onContextMenu”视为右键单击事件。它是一个HTML5.0标准。