Event.target
Uma referência ao objeto que enviou o evento. É diferente de event.currentTarget
quando o manipulador de eventos é chamado durante a fase de borbulhagem ou captura do evento.
Sintaxe
theTarget = event.target
Exemplo
A propriedade event.target
pode ser usada para implementar a delegação de eventos.
// Assumindo que existe uma variável 'list' contendo uma instância de um elemento ul de HTML.
function hide(e) {
// A menos que os itens da lista sejam separados por uma margem, e.target deve ser diferente de e.currentTarget
e.target.style.visibility = 'hidden';
}
list.addEventListener('click', hide, false);
// Se algum elemento (elemento <li> ou um link dentro de um elemento <li> por exemplo) for clicado, ele desaparecerá.
// Só requer um único listener para fazer isso.
Especificações
Specification | Status | Comment |
---|---|---|
DOM The definition of 'Event.target' in that specification. |
Padrão em tempo real | |
DOM4 The definition of 'Event.target' in that specification. |
Obsoleto | |
Document Object Model (DOM) Level 2 Events Specification The definition of 'Event.target' in that specification. |
Obsoleto | Initial definition |
Compatibilidade com navegadores
BCD tables only load in the browser