Найти ближайший соответствующий узел

const findClosestMatchingNode = (node, selector) => {
  for (let n = node; n.parentNode; n = n.parentNode)
    if (n.matches && n.matches(selector)) return n;
  return null;
};
findClosestMatchingNode(document.querySelector('span'), 'body'); // body
Браузер JavaScript