简体   繁体   中英

Javascript Event Delegation / bubbling

I want to disable all links with the class "nolink" in the body section for an SPA router. To achieve this I used event delegation which does not work very well with nested elements. (Simplified code below).

HTML:

<header id="header">
  <nobr>
    <a href="home" class="nolink">
      <img src="image.png">
      <span>Title</span>
    </a>
  </nobr>
  <ul>
    <li><a href="link1" class="nolink">Link 1</a></li>
    <li><a href="link2" class="nolink">Link 2</a></li>
    <li><a href="link3" class="nolink">Link 3</a></li>
    <li><a href="link4" class="nolink">Link 4</a></li>
  </ul>
</header>

JavaScript:

class Delegator {
  constructor(wrapper) {
    this.wrapper = wrapper || document.body;
  }

  add({ selector, event, callback }) {
    this.wrapper.addEventListener(event, e => {
      const target = e.target;
      if (target.matches(selector) /* bad code: */ || target.parentElement.matches(selector) || target.parentElement.parentElement.matches(selector)) {
        e.preventDefault();
        callback(e);
      }
    })
  }
}

function readyLink(event) {
  let href = event.target.href /* bad code: */ || event.target.parentElement.href || event.target.parentElement.parentElement.href;
  history.pushState(null, null, href);
  event.preventDefault();
  router.fetch(href.split("/").pop());
  router.route(href);
}

const router = new Router(...);
const bodyDelegator = new Delegator();
bodyDelegator.add({
  selector: `a.${nolink}`,
  event: "click",
  callback: readyLinks
});

It bothers me to have to refer to target.parentElements due to event routing when clicking an image or the span element. I want them to be affected by the add-function without having to specify a thousand parentElements for a possible future use.

Use Element.closest() to find if the element itself, or one of of it's parents matches the selector:

class Delegator {
  constructor(wrapper) {
    this.wrapper = wrapper || document.body;
  }

  add({ selector, event, callback }) {
    this.wrapper.addEventListener(event, e => {
      const target = e.target;

      if (target.closest(selector)) {
        e.preventDefault();
        callback(e);
      }
    })
  }
}

Passing the closest to the callback:

class Delegator {
  constructor(wrapper) {
    this.wrapper = wrapper || document.body;
  }

  add({ selector, event, callback }) {
    this.wrapper.addEventListener(event, e => {
      const target = e.target;

      const actual = target.closest(selector);

      if (actual) {
        e.preventDefault();
        callback(e, actual);
      }
    })
  }
}

And then readyLink can get the href from the actual element. I would still pass the event for other uses (calling preventDefault for example).

function readyLink(event, actualTarget) {
  let href = actualTarget.href;
  history.pushState(null, null, href);
  event.preventDefault();
  router.fetch(href.split("/").pop());
  router.route(href);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM