绑定事件以右键单击 [英] Bind event to right mouse click

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

问题描述

如何在禁用浏览器上下文菜单后右键单击可以触发某些操作?

How can I trigger some action with right click after disabling the browser context menu ?

我试过这个。 。

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        $('.alert').fadeToggle();
        return false;
    });
});


推荐答案

jQuery中没有内置oncontextmenu事件处理程序,但你可以这样做:

There is no built-in oncontextmenu event handler in jQuery, but you can do something like this:

$(document).ready(function(){ 
  document.oncontextmenu = function() {return false;};

  $(document).mousedown(function(e){ 
    if( e.button == 2 ) { 
      alert('Right mouse button!'); 
      return false; 
    } 
    return true; 
  }); 
});

基本上,我取消DOM元素的oncontextmenu事件以禁用浏览器上下文菜单,然后捕获

Basically I cancel the oncontextmenu event of the DOM element to disable the browser context menu, and then I capture the mousedown event with jQuery, and there you can know in the event argument which button has been pressed.

你可以尝试上面的例子此处

You can try the above example here.

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

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