【问题标题】:Bind to right-click with jQuery?用jQuery绑定右键单击?
【发布时间】:2014-09-13 14:33:05
【问题描述】:

我想将一个函数绑定到右键单击。 jQuery UI 可以做到这一点吗?

【问题讨论】:

    标签: jquery html jquery-ui


    【解决方案1】:

    虽然未在 http://api.jquery.com/bind/ 上列出,但 'contextmenu' 事件似乎有效

    $('.rightclickable').bind('contextmenu', function() {
        // right-click!
    });
    

    【讨论】:

    • 绑定已弃用。你现在应该使用.on()
    【解决方案2】:

    不是直接的,但您可以使用事件对象的which 属性检查在普通的mousedown 事件处理程序中按下了哪个鼠标按钮:

    $("#someElem").mousedown(function(e) {
        if(e.which == 3) {
            //Right click!
        }
    });
    

    这是上面的working example

    【讨论】:

      【解决方案3】:

      试试

      $(document).ready(function(){
          $(document).bind("contextmenu",function(e){
              //your
          });
      });
      

      【讨论】:

        【解决方案4】:
        $(document).bind('contextmenu',function(){
            return false;
        });
        $.fn.extend({
            "rightClick": function(fn){
                $(this).mousedown(function(e){
                    if (3 == e.which) {
                        fn();
                    }
                });
            }
        });
        $(function(){
            $('selector').rightClick(function(){
                // defined your right click event here!
            });
        }); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-26
          相关资源
          最近更新 更多