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


当前回答

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

其他回答

是的,它是!

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
}

使用jQuery库处理事件

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

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

这是最简单的方法,它可以在所有浏览器上运行,除了应用程序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>

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