AliasT
10/2/2018 - 4:40 AM

delegate.js

function delegate(source, targetSelector, type, fn, ...args) {
  source.addEventListener(type, function eventListener(event) {
    // 检查元素的target属性是否和target匹配
    for (let target = event.target; target !== event.currentTarget; target = target.parentNode) {
      if (target.matches(targetSelector)) {
        fn.call(target, event)
      }
    }
  }, ...args)
}