如何在JavaScript中捕获右键单击事件? [英] How can I capture the right-click event in JavaScript?

查看:113
本文介绍了如何在JavaScript中捕获右键单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止标准的上下文菜单,并手动处理右键单击事件。



如何完成?

解决方案

使用 oncontextmenu 事件。



一个例子:

 < div oncontextmenu =javascript:alert('success!'); return false;> 
Lorem Ipsum
< / div>

并使用事件监听器:

  el.addEventListener('contextmenu',function(ev){
ev.preventDefault();
alert('success!');
return false;
},false);

不要忘记返回false,否则标准的上下文菜单仍会弹出。 >

如果你要使用你写的函数,而不是 javascript:alert(Success!),记住在BOTH中返回false,并返回 oncontextmenu 属性。


I want to block the standard context menus, and handle the right-click event manually.

How is this done?

解决方案

Use the oncontextmenu event.

Here's an example:

<div oncontextmenu="javascript:alert('success!');return false;">
    Lorem Ipsum
</div>

And using event listeners:

el.addEventListener('contextmenu', function(ev) {
    ev.preventDefault();
    alert('success!');
    return false;
}, false);

Don't forget to return false, otherwise the standard context menu will still pop up.

If you are going to use a function you've written rather than javascript:alert("Success!"), remember to return false in BOTH the function AND the oncontextmenu attribute.

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

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