2016-11-07 92 views
0

我一直試圖觸發右鍵點擊,即使用戶離開點擊。我想讓左鍵單擊表現爲右鍵單擊使用JS或jQuery

我試過觸發器,triggerHandler,mousedown,但是我無法讓它工作。

我能夠捕捉點擊事件本身,但無法觸發上下文菜單。

任何想法?

+0

可你至少包括您在您的文章試圖代碼。 – Beginner

+0

你有沒有試過.contextmenu()https://api.jquery.com/contextmenu/ – Zorken17

+1

我想不明白!我告訴我的孩子們。 SO是爲你寫的代碼提供幫助,而不是代碼請求。發佈您迄今爲止所擁有的代碼示例。 – GillesC

回答

-1

使用以下代碼來反轉鼠標點擊。

$.extend($.ui.draggable.prototype, { 
     _mouseInit: function() { 
      var that = this; 
      if (!this.options.mouseButton) { 
       this.options.mouseButton = 1; 
      } 

      $.ui.mouse.prototype._mouseInit.apply(this, arguments); 

      if (this.options.mouseButton === 3) { 
       this.element.bind("contextmenu." + this.widgetName, function (event) { 
        if (true === $.data(event.target, that.widgetName + ".preventClickEvent")) { 
         $.removeData(event.target, that.widgetName + ".preventClickEvent"); 
         event.stopImmediatePropagation(); 
         return false; 
        } 
        event.preventDefault(); 
        return false; 
       }); 
      } 

      this.started = false; 
     }, 
     _mouseDown: function (event) { 

      // we may have missed mouseup (out of window) 
      (this._mouseStarted && this._mouseUp(event)); 

      this._mouseDownEvent = event; 

      var that = this, 
       btnIsLeft = (event.which === this.options.mouseButton), 
       // event.target.nodeName works around a bug in IE 8 with 
       // disabled inputs (#7620) 
       elIsCancel = (typeof this.options.cancel === "string" && event.target.nodeName ? $(event.target).closest(this.options.cancel).length : false); 
      if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) { 
       return true; 
      } 

      this.mouseDelayMet = !this.options.delay; 
      if (!this.mouseDelayMet) { 
       this._mouseDelayTimer = setTimeout(function() { 
        that.mouseDelayMet = true; 
       }, this.options.delay); 
      } 

      if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) { 
       this._mouseStarted = (this._mouseStart(event) !== false); 
       if (!this._mouseStarted) { 
        event.preventDefault(); 
        return true; 
       } 
      } 

      // Click event may never have fired (Gecko & Opera) 
      if (true === $.data(event.target, this.widgetName + ".preventClickEvent")) { 
       $.removeData(event.target, this.widgetName + ".preventClickEvent"); 
      } 

      // these delegates are required to keep context 
      this._mouseMoveDelegate = function (event) { 
       return that._mouseMove(event); 
      }; 
      this._mouseUpDelegate = function (event) { 
       return that._mouseUp(event); 
      }; 
      $(document) 
       .bind("mousemove." + this.widgetName, this._mouseMoveDelegate) 
       .bind("mouseup." + this.widgetName, this._mouseUpDelegate); 

      event.preventDefault(); 

      mouseHandled = true; 
      return true; 
     } 
    }); 

現在在函數調用時使用mouseButton:3點擊右鍵,1左鍵

0

要觸發鼠標右鍵單擊

function triggerRightClick(){ 
    var evt = new MouseEvent("mousedown", { 
     view: window, 
     bubbles: true, 
     cancelable: true, 
     clientX: 20, 
     button: 2 
    }); 
    some_div.dispatchEvent(evt); 
} 

要觸發的上下文菜單

function triggerContextMenu(){ 
    var evt = new MouseEvent("contextmenu", { 
     view: window 
    }); 
    some_div.dispatchEvent(evt); 
} 

這裏是斌:http://jsbin.com/rimejisaxi

爲了更好的參考/解釋:https://stackoverflow.com/a/7914742/1957036