Event.currentTarget
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Identifica o alvo atual para o evento quando o event percorre o DOM. O currentTarget sempre se refere ao elemento associado ao event handler, ao invés do event.target que identifica o elemento ao qual o evento ocorreu.
Exemplo
event.currentTarget
é bom para ser usado quando nós queremos associar o mesmo event handler para vários elementos.
js
function hide(e) {
e.currentTarget.style.visibility = "hidden";
console.log(e.currentTarget);
// Quando essa função é usada como um 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);
// Clique e faça os parágrafos desaparecerem
Especificações
Specification |
---|
DOM # ref-for-dom-event-currenttarget② |
Compatibilidade com navegadores
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
currentTarget |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.