fonction délégation

Utils.js

function $delegate(target, selector, type, handler, capture) {
const dispatchEvent = event => {
const targetElement = event.target;
const potentialElements = target.querySelectorAll(selector);
let i = potentialElements.length;

while (i--) {
if (potentialElements[i] === targetElement) {
handler.call(targetElement, event);
break;
}
}
};

  target.addEventListener(type, dispatchEvent, !!capture);
}

Utilisation

$delegate(target,"span","mouseover",show,true)