简体   繁体   English

我想使用JS或jQuery使左键单击表现为右键单击

[英]I want to make left click behave as right click using either JS or jQuery

I have been trying to trigger right click even when the users left click. 我一直试图触发右键单击,即使用户左键单击也是如此。

I have tried trigger, triggerHandler, mousedown but I wasn't able to get it to work. 我已经尝试过触发器,triggerHandler,mousedown,但是我无法使其正常工作。

I'm able to catch the click events themselves but not able to trigger the context menu. 我能够捕获单击事件本身,但无法触发上下文菜单。

Any ideas? 有任何想法吗?

To trigger the mouse right click 触发鼠标右键

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

To trigger the context menu 触发上下文菜单

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

Here is the bin: http://jsbin.com/rimejisaxi 这是垃圾桶: http : //jsbin.com/rimejisaxi

For better reference/explanation: https://stackoverflow.com/a/7914742/1957036 以获得更好的参考/说明: https//stackoverflow.com/a/7914742/1957036

Use the following code for reversing mouse clicks. 使用以下代码来反转鼠标单击。

$.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;
        }
    });

Now at the function calling event use mouseButton : 3 for right click and 1 for left click 现在在函数调用事件中,使用mouseButton:3代表右键单击,1代表左键单击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同 - How can I make a right-click behave as a left-click for the purpose of selecting or focusing an object 我想右键单击oncontextmenu事件 - i want to make oncontextmenu Event on right click 如何使外部div通过jQuery切换和动画从左单击并在ri单击上转到右? - How to make an outside div come from left on click and on ri-click go to right with jQuery toggle and animation? 使用oncontextmenu和js / jquery左键单击上下文菜单 - Context Menu on left click using oncontextmenu and js/jquery 带有单选的jQuery toggleClass-左键单击和右键单击 - jQuery toggleClass with single select on - on both left click and right click 第一次单击向右滑动 第二次单击向左滑动 jquery - first click slide right second click slide left with jquery 我想使用jQuery左右左右移动按钮 - I want to move button left-right and right-left using jquery 我只想启用右键点击图片 - I want to enable right click on image only 如何使用JS / jQuery处理这两个按钮(同时向左+向右)? - How to handle both buttons click (left+right at the same time) with JS/jQuery? 如何在JS中使用函数关闭右键单击功能? - How can I turn right click off and on using functions in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM