如何检测鼠标中键点击? [英] How to detect middle mouse button click?
问题描述
我在如何检测JavaScript中鼠标点击方面遇到的所有问题都与jQuery有关,但我想知道如何使用常规JavaScript检测鼠标中键点击。我尝试使用 onClick()
,但它似乎只适用于鼠标左键。
All of the questions I've seen on how to detect a middle mouse click in JavaScript are related to jQuery, but I'm wondering how I can detect middle mouse button clicks with regular JavaScript. I tried using onClick()
, but it only appears to work for the left mouse button.
是否有JavaScript函数可以检测左键和中键鼠标按键,如果没有,可以检测到鼠标中键点击?
Is there a JavaScript function that can detect both left and middle mouse button clicks or, if not, that can detect middle mouse button clicks?
我问的原因是我想制作单击链接时的函数调用,无论是使用鼠标左键还是鼠标中键。
The reason I ask is that I want to make a function call when links are clicked, irregardless of whether the left or middle mouse button was used.
推荐答案
onclick不依赖于一个鼠标,但更多关于目标元素本身。
onclick is not tied to a mouse, but more on the target element itself.
以下是如何检测元素是否被中间点击:
Here's how to detect whether an element is middle clicked:
document.body.onclick = function (e) {
if (e && (e.which == 2 || e.button == 4 )) {
console.log('middleclicked')
}
}
这篇关于如何检测鼠标中键点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!