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.
La propiedad target
de la interfaz del event.currentTarget
es una referencia al objeto en el cual se lanzo el evento. Es diferente de event.currentTarget
donde el controlador de eventos (event handler) es llamado durante la fase de bubbling or capturing del evento.
Sintaxis
const theTarget = algunEvento.target
Ejemplo
La propiedad event.target
puede ser usada para implementar una delegación del evento.
js
// Crear una lista
const ul = document.createElement("ul");
document.body.appendChild(ul);
const li1 = document.createElement("li");
const li2 = document.createElement("li");
ul.appendChild(li1);
ul.appendChild(li2);
function hide(e) {
// e.target se refiere elemento clickado <li>
// Esto es diferente de e.currentTarget, que se referiría al padre <ul> en este contexto
e.target.style.visibility = "hidden";
}
// Incluir el 'listener' a la lista
// Se ejecutará cuando se haga click en cada <li>
ul.addEventListener("click", hide, false);
Especificaciones
Specification |
---|
DOM # ref-for-dom-event-target③ |
Compatibilidad con 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.