右击是Javascript事件吗?如果是,我该如何使用它?


是的,它是!

function doSomething(e) {
    var rightclick;
    if (!e) var e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alert('Rightclick: ' + rightclick); // true or false
}

没有,但是你可以检测到在"onmousedown"事件中使用了什么鼠标按钮…然后从那里判断它是否是一个“右键”。


是的,它是一个javascript鼠标下拉事件。有一个jQuery插件也可以做到这一点


正如其他人所提到的,可以通过通常的鼠标事件(鼠标下拉、鼠标上拉、单击)来检测鼠标右键。但是,如果您在弹出右键菜单时寻找触发事件,那么就找错地方了。右键单击/上下文菜单也可以通过键盘(shift+F10或上下文菜单键在Windows和一些Linux)访问。在这种情况下,你正在寻找的事件是oncontextmenu:

window.oncontextmenu = function ()
{
    showCustomMenu();
    return false;     // cancel default menu
}

至于鼠标事件本身,浏览器为事件对象设置了一个属性,可以从事件处理函数中访问:

document.body.onclick = function (e) {
    var isRightMB;
    e = e || window.event;

    if ("which" in e)  // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
        isRightMB = e.which == 3; 
    else if ("button" in e)  // IE, Opera 
        isRightMB = e.button == 2; 

    alert("Right mouse button " + (isRightMB ? "" : " was not") + "clicked!");
} 

窗口。oncontextmenu - MDC


看看下面的jQuery代码:

$("#myId").mousedown(function(ev){
      if(ev.which == 3)
      {
            alert("Right mouse button clicked on element with id myId");
      }
});

的值为:

1为左键 2为中间按钮 3为右键


是的,虽然w3c说右键点击可以通过点击事件检测到,onClick不是通过在通常的浏览器中单击右键触发的。

事实上,右键只触发onMouseDown onMouseUp和onContextMenu。

因此,您可以将“onContextMenu”视为右键单击事件。它是一个HTML5.0标准。


使用jQuery库处理事件

$(window).on("contextmenu", function(e)
{
   alert("Right click");
})

是的,oncontextmenu可能是最好的选择,但请注意,它在鼠标向下时触发,而单击将在鼠标向上时触发。

其他相关的问题是关于双击右键的——显然除了手动计时器检查外,不支持双击右键。您可能希望能够右双击的一个原因是,如果您需要/想要支持左手鼠标输入(按钮反转)。浏览器实现似乎对我们应该如何使用可用的输入设备做了很多假设。


您可以使用事件窗口。Oncontextmenu,例如:

窗口。Oncontextmenu = function () { alert(右键) } <h1>请右键点击这里!< / h1 >


下面的代码使用jQuery生成一个基于默认鼠标下拉和鼠标上拉事件的自定义右键单击事件。 它考虑到以下几点:

鼠标悬停时触发 仅当鼠标按下之前的同一元素时触发 这段代码特别适用于JFX Webview(因为上下文菜单事件不会在那里触发) 当键盘上的上下文菜单键被按下时,它不会触发(就像on('contextmenu',…)的解决方案一样)

$(function () { // global rightclick handler - trigger custom event "rightclick" var mouseDownElements = []; $(document).on('mousedown', '*', function(event) { if (event.which == 3) { mouseDownElements.push(this); } }); $(document).on('mouseup', '*', function(event) { if (event.which == 3 && mouseDownElements.indexOf(this) >= 0) { $(this).trigger('rightclick'); } }); $(document).on('mouseup', function() { mouseDownElements.length = 0; }); // disable contextmenu $(document).on('contextmenu', function(event) { event.preventDefault(); }); }); // Usage: $('#testButton').on('rightclick', function(event) { alert('this was a rightclick'); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="testButton">Rightclick me</button>


如果你想调用函数,而右击事件意味着我们可以使用以下

 <html lang="en" oncontextmenu="func(); return false;">
 </html>

<script>
function func(){
alert("Yes");
}
</script>

最简单的右击方式是使用

 $('classx').on('contextmenu', function (event) {

 });

然而,这不是跨浏览器的解决方案,浏览器的行为不同,特别是firefox和IE。我推荐下面的跨浏览器解决方案

$('classx').on('mousedown', function (event) {
    var keycode = ( event.keyCode ? event.keyCode : event.which );
    if (keycode === 3) {
       //your right click code goes here      
    }
});

这是我的工作

if (evt.xa.which == 3) 
{
    alert("Right mouse clicked");
}

如果您想检测鼠标右键单击,就不应该使用MouseEvent。哪个属性是不标准的,浏览器之间有很大的不兼容性。你应该使用MouseEvent.button。它返回一个表示给定按钮的数字:

0:主按钮按下,通常是左键或未初始化状态 1:辅助按钮按下,通常是车轮按钮或中间按钮(如果有) 2:二级按钮按下,通常是右键 3:第四个按钮,通常是浏览器返回按钮 4:第五个按钮,通常是浏览器前进按钮

MouseEvent。按钮处理的输入类型比标准鼠标更多:

按钮的配置可能与标准不同 “从左到右”的布局。配置为左撇子使用的鼠标可以 将按钮动作反转。有些指向设备只有一个 按钮,并使用键盘或其他输入机制来指示主, 二级、辅助性等。其他的可能有许多映射到的按钮 不同的功能和按钮值。

参考:

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button


这是最简单的方法,它可以在所有浏览器上运行,除了应用程序web视图,如(CefSharp铬等…). 我希望我的代码能帮助到你,祝你好运!

const contentToRightClick=document.querySelector("div#contentToRightClick"); //const contentToRightClick=window; //If you want to add it into the whole document contentToRightClick.oncontextmenu=function(e){ e=(e||window.event); e.preventDefault(); console.log(e); return false; //Remove it if you want to keep the default contextmenu } div#contentToRightClick{ background-color: #eee; border: 1px solid rgba(0,0,0,.2); overflow: hidden; padding: 20px; height: 150px; } <div id="contentToRightClick">Right click on the box !</div>


窗口。Oncontextmenu =函数(e) { e.preventDefault () alert(右键) } <h1>请右键点击这里!< / h1 >


您可能想尝试以下属性:

按钮- (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


大多数使用mouseup或上下文菜单事件的给定解决方案在每次鼠标右键上升时触发,但它们不会检查鼠标右键之前是否下降。


如果您正在寻找一个真正的右键单击事件,该事件仅在同一元素中按下并释放鼠标按钮时触发,那么您应该使用auxclick事件。由于这将触发每个非主鼠标按钮,您还应该通过检查按钮属性过滤其他事件。

窗口。addEventListener("auxclick", (event) => { 如果事件。button === 2) alert("Right click"); });

你也可以通过在JavaScript开头添加以下代码来创建自己的右键事件:

{
  const rightClickEvent = new CustomEvent('rightclick', { bubbles: true });
  window.addEventListener("auxclick", (event) => {
    if (event.button === 2) {
      event.target.dispatchEvent(rightClickEvent);
    }
  });
}

然后你可以通过addEventListener方法监听右键事件,如下所示:

your_element.addEventListener("rightclick", your_function);

在MDN上阅读更多关于auxclick事件的信息。


是的,这是一个Javascript事件,你可以用下面的代码测试它。

<div id="contextArea">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  </div>



    <script>
    var contextarea = $("#contextArea");
    
    contextarea.contextmenu(function (e) {
      e.preventDefault();
      console.log("right click from p tag");
    })
    </script>

添加一个e.preventDefault来阻止菜单出现(以防你不想要它) 窗口。Oncontextmenu =函数(e) { e.preventDefault (); 警报(“你点击”); } <标题>右键单击< / h1 >


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>