元素的兄弟节点数组
返回一个包含给定元素的所有兄弟节点的数组。
- 使用
Node.parentNode和Node.childNodes来获取包含在元素的父节点中的所有元素的NodeList。 - 使用扩展运算符 (
...) 和Array.prototype.filter()将其转换为数组,并从中删除给定的元素。
const getSiblings = el =>
[...el.parentNode.childNodes].filter(node => node !== el);
getSiblings(document.querySelector('head')); // ['body']