GlobalEventHandlers
event handler
onpointerdown
is used to specify the event handler for the
pointerdown
event, which is fired when the pointing device is initially pressed. This event can be sent to
Window
,
Document
,和
元素
对象。
This is functionally equivalent to the
mousedown
event when generated due to user activity with a mouse or mouse-compatible device. If the
pointerdown
event isn't canceled through a call to
preventDefault()
, most user agents will fire a
mousedown
event, so that sites not using pointer events will work.
还可以使用
addEventListener()
to add a listener for
pointerdown
事件。
target.onpointerdown = downHandler; var downHandler = target.onpointerdown;
A
函数
to handle the
pointerdown
事件为
target
元素
,
Document
,或
Window
. It receives as input the
PointerEvent
describing the
pointerdown
事件。
This example demonstrates how to watch for and act upon
pointerdown
events using
onpointerdown
. You could also use
addEventListener()
, of course.
First, let's look at the JavaScript code that handles the
pointerdown
事件。
var targetBox = document.getElementById("target");
targetBox.onpointerdown = handleDown;
function handleDown(evt) {
var action;
switch(evt.pointerType) {
case "mouse":
action = "clicking";
break;
case "pen":
action = "tapping";
break;
case "touch":
action = "touching";
break;
默认:
action = "interacting with";
break;
}
targetBox.innerHTML = "<strong>Thanks for " + action + " me!</strong>";
evt.preventDefault();
}
This simply uses
onpointerdown
to establish the function
handleDown()
as the event handler for pointer down events.
handleDown()
function, in turn, looks at the value of
pointerType
to determine what kind of pointing device was used, then uses that information to customize a string to replace the contents of the target box.
Then the event's
preventDefault()
method is called to ensure that the
mousedown
event isn't triggered, potentially causing events to be handled twice if we had a handler for those events in case Pointer Event support is missing.
We also have a handler for
pointerup
事件:
targetBox.onpointerup = handleUp;
function handleUp(evt) {
targetBox.innerHTML = "Tap me, click me, or touch me!";
evt.preventDefault();
}
This code's job is to just restore the original text into the target box after the user's interaction with the element ends (for example, when they release the mouse button, or when they lift the stylus or finger from the screen).
In addition, the event's
preventDefault()
method is called to ensure that the
mouseup
event isn't triggered unnecessarily.
The HTML is extremely simple:
<div id="target"> Tap me, click me, or touch me! </div>
The CSS simply sets up the appearance of the target, and doesn't affect its functionality at all.
#target {
width: 400px;
height: 30px;
text-align: center;
font: 16px "Open Sans", "Helvetica", sans-serif;
color: white;
background-color: blue;
border: 2px solid darkblue;
cursor: pointer;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
The resulting output is shown below. Try tapping, clicking, or touching the box and see what happens. For full effect, try it with a variety of pointer types.
| 规范 | 状态 | 注释 |
|---|---|---|
|
指针事件 – 2 级
The definition of 'onpointerdown' in that specification. |
推荐 | Non-stable version |
|
指针事件
The definition of 'onpointerdown' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
onpointerdown
|
Chrome 55 |
Edge
12
|
Firefox
59
|
IE
11
|
Opera ? | Safari No | WebView Android 55 | Chrome Android 55 | Firefox Android 29 | Opera Android ? | Safari iOS No | Samsung Internet Android 6.0 |
完整支持
不支持
兼容性未知
用户必须明确启用此特征。
使用非标名称。
Document: pointerdown
event
HTMLElement: pointerdown
event
GlobalEventHandlers
onabort
onanimationcancel
onanimationend
onanimationiteration
onauxclick
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondurationchange
onended
onerror
onfocus
onformdata
ongotpointercapture
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onwheel
BeforeUnloadEvent
DOMStringMap
ErrorEvent
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers