An element receives a
click
event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.
| 冒泡 | Yes |
|---|---|
| 可取消 | Yes |
| 接口 |
MouseEvent
|
| 事件处理程序特性 |
onclick
|
If the button is pressed on one element and the pointer is moved outside the element before the button is released, the event is fired on the most specific ancestor element that contained both elements.
click
fires after both the
mousedown
and
mouseup
events have fired, in that order.
MouseEvent
object passed into the event handler for
click
有它自己的
detail
property set to the number of times the
target
was clicked. In other words,
detail
will be 2 for a double-click, 3 for triple-click, and so forth. This counter resets after a short interval without any clicks occurring; the specifics of how long that interval is may vary from browser to browser and across platforms. The interval is also likely to be affected by user preferences; for example, accessibility options may extend this interval to make it easier to perform multiple clicks with adaptive interfaces.
Internet Explorer 8 & 9 suffer from a bug where elements with a computed
background-color
of
transparent
that are overlaid on top of other element(s) won't receive
click
events. Any
click
events will be fired at the underlying element(s) instead. See
this live example
for a demonstration.
Known workarounds for this bug:
background-color
:
rgba
(0,0,0,0)
opacity
: 0
and an explicit
background-color
除了
transparent
filter
: alpha(opacity=0);
and an explicit
background-color
除了
transparent
Safari Mobile 7.0+ (and likely earlier versions too)
suffers from a bug
where
click
events aren't fired on elements that aren't typically interactive (e.g.
<div>
) and which also don't have event listeners directly attached to the elements themselves (i.e.
event delegation
is being used). See
this live example
for a demonstration. See also
Safari's docs on making elements clickable
和
definition of "clickable element"
.
Known workarounds for this bug:
cursor
: pointer;
on the element or any of its ancestors.
onclick="void(0)"
attribute to the element or any of its ancestors up to but not including
<body>
.
<a>
) instead of one that isn't typically interactive (e.g.,
<div>
).
click
event delegation
.
Safari Mobile considers the following elements to be typically interactive (and thus they aren't affected by this bug):
<a>
(but it must have an
href
)
<area>
(but it must have an
href
)
<button>
<img>
<input>
<label>
(but it must be associated with a form control)
<textarea>
This example displays the number of consecutive clicks on a
<button>
.
<button>Click</button>
const button = document.querySelector('button');
button.addEventListener('click', event => {
button.innerHTML = `Click count: ${event.detail}`;
});
Try making rapid, repeated clicks on the button to increase the click count. If you take a break between clicks, the count will reset.
| 规范 | 状态 |
|---|---|
| UI Events | 工作草案 |
|
DOM (文档对象模型) 3 级事件规范
The definition of 'click' in that specification. |
过时 |
|
DOM (文档对象模型) 2 级事件规范
The definition of 'click' in that specification. |
过时 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
click
event
|
Chrome 1 | Edge 12 |
Firefox
6
|
IE 9 | Opera 11.6 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 6 | Opera Android 12.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
| On disabled form elements |
Chrome
Yes
|
Edge
79
|
Firefox No |
IE
Yes
|
Opera
Yes
|
Safari ? |
WebView Android
Yes
|
Chrome Android
Yes
|
Firefox Android No |
Opera Android
Yes
|
Safari iOS ? |
Samsung Internet Android
Yes
|
完整支持
不支持
兼容性未知
见实现注意事项。
元素
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