【问题标题】:Bind event to right mouse click将事件绑定到鼠标右键
【发布时间】:2010-10-16 22:32:28
【问题描述】:

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

我试过这个。 . .

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

【问题讨论】:

标签: jquery css


【解决方案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 事件,你可以在事件参数中知道按下了哪个按钮。

你可以试试上面的例子here

【讨论】:

  • $(document)[0]document不一样吗?
  • @configurator,是的,这是一个错字。谢谢:)
  • "contextmenu" 现已支持!
  • 这似乎在 Firefox 中不起作用 - 只有 chrome。 e.button 的数值很可能是特定于浏览器或硬件的
  • 最好使用 e.which(而不是 e.button),它会为所有浏览器提供相同的值。
【解决方案2】:

函数返回过早。我在下面的代码中添加了注释:

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        return false;
        $('.alert').fadeToggle(); // this line never gets called
    });
});

尝试将return false; 与下一行交换。

【讨论】:

  • "asked Apr 1 '09" - 我认为这并不重要。 ;)
  • @zourtney 是什么让你得出这样的结论? jQuery 文档没有提到任何关于支持“上下文菜单”的内容...
【解决方案3】:

只需使用事件处理程序。像这样的东西应该可以工作:

$('.js-my-element').bind('contextmenu', function(e) {
     e.preventDefault();
     alert('The eventhandler will make sure, that the contextmenu dosn't appear.');
});

【讨论】:

    【解决方案4】:

    我在这里找到了这个答案,我正在这样使用它。

    我的库中的代码:

    $.fn.customContextMenu = function(callBack){
        $(this).each(function(){
            $(this).bind("contextmenu",function(e){
                 e.preventDefault();
                 callBack();
            });
        }); 
    }
    

    我页面脚本中的代码:

    $("#newmagazine").customContextMenu(function(){
        alert("some code");
    });
    

    【讨论】:

      【解决方案5】:
      document.oncontextmenu = function() {return false;}; //disable the browser context menu
      $('selector-name')[0].oncontextmenu = function(){} //set jquery element context menu 
      

      【讨论】:

      • document.oncontextmenu = function() {return false;}; 根本不起作用。上下文菜单仍然出现在浏览器中(Chrome 66)
      【解决方案6】:

      contextmenu 是一个事件吗?

      我会使用onmousedownonclick 然后获取MouseEventbutton 属性来确定按下了哪个按钮(0 = 左,1 = 中,2 = 右)。

      【讨论】:

        【解决方案7】:

        要在页面的所有图像上禁用右键单击上下文菜单,只需执行以下操作:

        jQuery(document).ready(function(){
            // Disable context menu on images by right clicking
            for(i=0;i<document.images.length;i++) {
                document.images[i].onmousedown = protect;
            }
        });
        
        function protect (e) {
            //alert('Right mouse button not allowed!');
            this.oncontextmenu = function() {return false;};
        }
        

        【讨论】:

          【解决方案8】:

          .contextmenu 方法:-

          如下尝试

          <div id="wrap">Right click</div>
          
          <script>
          $('#wrap').contextmenu(function() {
            alert("Right click");
          });
          </script>
          

          .mousedown 方法:-

          $('#wrap').mousedown(function(event) {
          
                  if(event.which == 3){
                      alert('Right Mouse button pressed.');
                  }  
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-01-26
            • 1970-01-01
            • 2011-05-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多