function closest(el, selector) {
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector
let element = el
while (element && element.removeChild) {
if (matchesSelector.call(element, selector)) {
return element
}
element = element.parentElement
}
return null
}
const delegate = document.querySelector('.js-delegate')
delegate.addEventListener('click', (e) => {
const target = closest(e.target, '.target1')
if (!target) return
// todo
})
delegate.addEventListener('click', (e) => {
const target = closest(e.target, '.target2')
if (!target) return
// todo
})
delegate.addEventListener('click', (e) => {
const target = closest(e.target, '.target3')
if (!target) return
// todo
})