cancelable
只读特性在
事件
interface indicates whether the event can be canceled, and therefore prevented as if the event never happened.
If the event is
not
cancelable, then its
cancelable
property will be
false
and the event listener cannot stop the event from occurring.
Event listeners that handle multiple kinds of events may want to check
cancelable
before invoking their
preventDefault()
方法。
Most browser-native events that can be canceled are the ones that result from the user interacting with the page. Canceling the
click
,
scroll
,或
beforeunload
events would prevent the user from clicking on something, scrolling the page, or navigating away from the page, respectively.
Custom events created by other JavaScript code control if they can be canceled when they are created.
bool = event.cancelable;
The result is a
布尔
,其是
true
if the event can be canceled.
For example, browser vendors are proposing that the
wheel
event can only be canceled
the first time the listener is called
— any following
wheel
events cannot be canceled.
function preventScrollWheel(event) {
if (typeof event.cancelable !== 'boolean' || event.cancelable) {
// The event can be canceled, so we do so.
event.preventDefault();
} else {
// The event cannot be canceled, so it is not safe
// to call preventDefault() on it.
console.warn(`The following event couldn't be canceled:`);
console.dir(event);
}
}
document.addEventListener('wheel', preventScrollWheel);
Whether an event can be canceled or not is something that's determined when that event is initialized.
To cancel an event, call the
preventDefault()
method on the event. This keeps the implementation from executing the default action that is associated with the event.
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Event.cancelable' in that specification. |
实时标准 | |
|
DOM (文档对象模型) 2 级事件规范
The definition of 'Event.cancelable' in that specification. |
过时 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
cancelable
|
Chrome Yes | Edge 12 | Firefox Yes | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
兼容性未知
事件
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument