JS - 获取元素的所有祖先元素 雾徊 2023-08-23 59 阅读1分钟 const getAncestors = el => { let ancestors = []; while (el) { ancestors.unshift(el); el = el.parentNode; } return ancestors; }; getAncestors(document.querySelector('nav')); // [document, html, body, header, nav]