Event.currentTarget
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.
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
Especificação | Status | Comentário |
---|---|---|
DOM The definition of 'Event.currentTarget' in that specification. |
Padrão em tempo real | |
DOM4 The definition of 'Event.currentTarget' in that specification. |
Obsoleto | |
Document Object Model (DOM) Level 3 Events Specification The definition of 'current event target' in that specification. |
Obsoleto | |
Document Object Model (DOM) Level 2 Events Specification The definition of 'Event.currentTarget' in that specification. |
Obsoleto | Definição incial |
Compatibilidade com navegadores
BCD tables only load in the browser