是否右键单击Javascript事件?

问题描述:

是否右键单击Javascript事件?如果是这样,我该如何使用?

Is right click a Javascript event? If so, how do I use it?

正如其他人所提到的,可以检测到鼠标右键通过通常的鼠标事件(mousedown,mouseup ,单击)。但是,如果您在启动右键单击菜单时正在寻找触发事件,那么您正在查找错误的位置。右键单击/上下文菜单也可通过键盘访问(在Windows和某些Linux上使用shift + F10或上下文菜单键)。在这种情况下,您要查找的事件是 oncontextmenu

As others have mentioned, the right mouse button can be detected through the usual mouse events (mousedown, mouseup, click). However, if you're looking for a firing event when the right-click menu is brought up, you're looking in the wrong place. The right-click/context menu is also accessible via the keyboard (shift+F10 or context menu key on Windows and some Linux). In this situation, the event that you're looking for is oncontextmenu:

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

至于鼠标事件本身,浏览器设置属性到可从事件处理函数访问的事件对象:

As for the mouse events themselves, browsers set a property to the event object that is accessible from the event handling function:

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!");
} 

window.oncontextmenu - MDC