currentTarget
只读特性在
事件
interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to
Event.target
, which identifies the element on which the event occurred and which may be its descendant.
var currentEventTarget = event.currentTarget;
Event.currentTarget
is interesting to use when attaching the same event handler to several elements.
function hide(e){
e.currentTarget.style.visibility = 'hidden';
console.log(e.currentTarget);
// When this function is used as an event handler: this === e.currentTarget
}
var ps = document.getElementsByTagName('p');
for(var i = 0; i < ps.length; i++){
// Console: print the clicked <p> element
ps[i].addEventListener('click', hide, false);
}
// Console: print <body>
document.body.addEventListener('click', hide, false);
// Click around and make paragraphs disappear
注意:
值
event.currentTarget
is
only
available while the event is being handled. If you
console.log()
the
event
object, storing it in a variable, and
then
look for the
currentTarget
key in the console, its value will be
null
. Instead, you can either directly
console.log(event.currentTarget)
to be able to view it in the console or use the
debugger
statement, which will pause the execution of your code thus showing you the value of
event.currentTarget
.
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
currentTarget
|
Chrome 1 | Edge 12 | Firefox 1 |
IE
9
|
Opera 7 | Safari 10 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 10 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
事件
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