是否右键单击Javascript事件? [英] Is right click a Javascript event?

查看:162
本文介绍了是否右键单击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

这篇关于是否右键单击Javascript事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆