Event.target
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.
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.
js
// 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 |
---|
DOM # ref-for-dom-event-target③ |
Compatibilidade com navegadores
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
target |
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.