简体   繁体   English

是否可以使用Javascript触发鼠标中键事件?

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

I want to trigger mouse middle click event using javascript.我想使用 javascript 触发鼠标中键事件。 Is it possible to trigger mouse middle click using Javascript?是否可以使用Javascript触发鼠标中键?

I want it because it is pasting last selected object from clipboard.我想要它,因为它正在粘贴剪贴板中最后选择的对象。

Thanks, Jimit谢谢,吉米特

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": 2 });
    jQuery(".selector")[0].dispatchEvent( middleClick );
}

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.有两个属性用于确定哪个鼠标按钮被点击:which 和 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.要安全地检测鼠标按钮,您必须使用 mousedown 或 mouseup 事件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM