Is it possible to trigger mouse middle click event using Javascript?

守給你的承諾、 提交于 2019-12-04 22:19:24

问题


I want to trigger mouse middle click event using javascript. Is it possible to trigger mouse middle click using Javascript?

I want it because it is pasting last selected object from clipboard.

Thanks, Jimit


回答1:


Had the same question, did a lot of digging, and this is what I ended up using:

if ( window.CustomEvent ) {
    var middleClick = new MouseEvent( "click", { "button": 1, "which": 1 });
    jQuery(".selector")[0].dispatchEvent( middleClick );
}



回答2:


You can use

event.button

to identify which mouse button was clicked.

Returns an integer value indicating the button that changed state.

* 0 for standard 'click', usually left button
* 1 for middle button, usually wheel-click
* 2 for right button, usually right-click

  Note that this convention is not followed in Internet Explorer: see 
  QuirksMode for details.

The order of buttons may be different depending on how the pointing device has been configured.

Also read

Which mouse button has been clicked?

There are two properties for finding out which mouse button has been clicked: which and button. Please note that these properties don’t always work on a click event. To safely detect a mouse button you have to use the mousedown or mouseup events.



来源:https://stackoverflow.com/questions/22243712/is-it-possible-to-trigger-mouse-middle-click-event-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!