小编典典

将事件绑定到鼠标右键

css

禁用浏览器上下文菜单后,如何右键单击触发一些动作?

我试过了。。。

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

阅读 421

收藏
2020-05-16

共1个答案

小编典典

jQuery中没有内置的oncontextmenu事件处理程序,但是您可以执行以下操作:

$(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事件以禁用浏览器上下文菜单,然后使用jQuery捕获mousedown事件,您可以在事件参数中知道按下了哪个按钮。

2020-05-16