auxclick
event is fired at an
元素
when a non-primary pointing device button (any mouse button other than the primary—usually leftmost—button) has been pressed and released both within the same element.
auxclick
is fired after the
mousedown
and
mouseup
events have been fired, in that order.
| 冒泡 | Yes |
|---|---|
| 可取消 | Yes |
| 接口 |
MouseEvent
|
| 事件处理程序特性 |
onauxclick
|
For the vast majority of browsers that map middle click to opening a link in a new tab, including Firefox, it is possible to cancel this behavior by calling
preventDefault()
from within an
auxclick
event handler.
When listening for
auxclick
events originating on elements that do not support input or navigation, you will often want to explicitly prevent other default actions mapped to the down action of the middle mouse button. On Windows this is usually autoscroll, and on macOS and Linux this is usually clipboard paste. This can be done by preventing the default behaviour of the
mousedown
or
pointerdown
事件。
Additionally, you may need to avoid opening a system context menu after a right click. Due to timing differences between operating systems, this too is not a preventable default behavior of
auxclick
. Instead, this can be done by preventing the default behavior of the
contextmenu
事件。
In this example we define functions for two event handlers —
onclick
and
onauxclick
. The former changes the color of the button background, while the latter changes the button foreground (text) color. You also can see the two functions in action by trying the demo out with a multi-button mouse (
see it live on GitHub
; also
see the source code
).
let button = document.querySelector('button');
let html = document.querySelector('html');
function random(number) {
return Math.floor(Math.random() * number);
}
function randomColor() {
return `rgb(${random(255)}, ${random(255)}, ${random(255)})`;
}
button.onclick = function() {
button.style.backgroundColor = randomColor();
};
button.onauxclick = function(e) {
e.preventDefault();
button.style.color = randomColor();
}
button.oncontextmenu = function(e) {
e.preventDefault();
}
Notice that in addition to capturing the
auxclick
event using
onauxclick
,
contextmenu
event is also captured, and
preventDefault()
called on that event, in order to prevent the context menu from popping up after the color change is applied.
<button><h1>Click me!</h1></button>
html {
height: 100%;
overflow: hidden;
}
body {
height: inherit;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
button {
border: 0;
background-color: white;
font-size: 8vw;
display: block;
width: 100%;
height: 100%;
}
h1 {
letter-spacing: 0.5rem;
}
注意
: If you are using a three-button mouse, you'll notice that the
onauxclick
handler is run when any of the non-left mouse buttons are clicked (usually including any "special" buttons on gaming mice).
| 规范 | 状态 |
|---|---|
|
UI Events
The definition of 'auxclick' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
auxclick
event
|
Chrome 55 | Edge ≤79 |
Firefox
53
注意事项
|
IE 不支持 No | Opera 42 | Safari 不支持 No | WebView Android 55 | Chrome Android 55 | Firefox Android 53 | Opera Android 42 | Safari iOS 不支持 No | Samsung Internet Android 6.0 |
完整支持
不支持
见实现注意事项。
元素
accessKey
属性
childElementCount
children
classList
className
clientHeight
clientLeft
clientTop
clientWidth
currentStyle
firstElementChild
id
innerHTML
lastElementChild
localName
名称
namespaceURI
nextElementSibling
onfullscreenchange
onfullscreenerror
openOrClosedShadowRoot
outerHTML
part
prefix
previousElementSibling
runtimeStyle
scrollHeight
scrollLeft
scrollLeftMax
scrollTop
scrollTopMax
scrollWidth
shadowRoot
slot
tabStop
tagName
after()
animate()
append()
attachShadow()
before()
closest()
computedStyleMap()
createShadowRoot()
getAnimations()
getAttribute()
getAttributeNames()
getAttributeNode()
getAttributeNodeNS()
getAttributeNS()
getBoundingClientRect()
getClientRects()
getElementsByClassName()
getElementsByTagName()
getElementsByTagNameNS()
hasAttribute()
hasAttributeNS()
hasAttributes()
hasPointerCapture()
insertAdjacentElement()
insertAdjacentHTML()
insertAdjacentText()
matches()
msZoomTo()
prepend()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
releasePointerCapture()
remove()
removeAttribute()
removeAttributeNode()
removeAttributeNS()
replaceChildren()
replaceWith()
requestFullscreen()
requestPointerLock()
scroll()
scrollBy()
scrollIntoView()
scrollIntoViewIfNeeded()
scrollTo()
setAttribute()
setAttributeNode()
setAttributeNodeNS()
setAttributeNS()
setCapture()
setPointerCapture()
toggleAttribute()
afterscriptexecute
auxclick
blur
click
compositionend
compositionstart
compositionupdate
contextmenu
copy
cut
dblclick
DOMActivate
DOMMouseScroll
error
focus
focusin
focusout
fullscreenchange
fullscreenerror
gesturechange
gestureend
gesturestart
keydown
keypress
keyup
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
mousewheel
MozMousePixelScroll
msContentZoom
MSGestureChange
MSGestureEnd
MSGestureHold
MSGestureStart
MSGestureTap
MSInertiaStart
MSManipulationStateChanged
overflow
paste
scroll
select
show
touchcancel
touchend
touchmove
touchstart
underflow
webkitmouseforcechanged
webkitmouseforcedown
webkitmouseforceup
webkitmouseforcewillbegin
wheel