How to replace the left mouse click, click the middle mouse button (scroll), that is, press the left button but the action is performed as if you click the middle mouse button.

e.preventDefault(); var $link = $(this).find('a'), mouseEventMiddle = new MouseEvent( "click", { "button": 1, "which": 2 }); $link[0].dispatchEvent(mouseEventMiddle); return false; 

    1 answer 1

     function logMouse(e) { var evt = e.type; while (evt.length < 11) evt += ' '; console.log(evt + " which=" + e.which + " button=" + e.button); return false; } function cloneObject(obj) { var key, clone = {}; for (key in obj) if (obj.hasOwnProperty(key)) clone[key] = obj[key]; return clone; } $(".jaja").on("click", function(e) { var newE = cloneObject(e); e.preventDefault(); newE.button = 1; newE.which = 2; newE.originalEvent.button = 1; newE.originalEvent.which = 2; newE.originalEvent.defaultPrevented = false; newE.isDefaultPrevented = false; console.log("newE:" + newE.button); console.log("e:" + e.button); console.log(newE.button === e.button); e = new MouseEvent("click", newE); console.log("Щёлк! И уже средняя кнопка"); logMouse(e); // тут евент будет уже с другими значениями button и which, но все остальные поля будут сохранены благодаря копированию объекта return false; }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input onmousedown="return logMouse(event)" onmouseup="return logMouse(event)" onclick="return logMouse(event)" oncontextmenu="return logMouse(event)" ondblclick="return logMouse(event)" value="Кликни меня левой или правой кнопкой мыши" type="button"> <input class="jaja" type="button" value="Щелк"> 

    I did it only this way - but it does not cause the behavior of the average mouse - only an “illusion”. We still need to think about how to "substitute" the behavior so that the "scroll" icon appears (which appears when you press the middle button)

    • and if to change input class = "jaja" on and class = "jaja". if you click on the link with the left button, a new tab will open and switch to it, and if the middle one opens a new tab in the background (it does not switch to it). - Michael
    • Something I have not rolled out this option. Switching to a link (with href) changes the triggering of the left button, but doesn’t replace it by pressing the middle button with its event. We must look further - alexoander