jiaochunxiao / fe-blog

好好学习,天天向上
1 stars 1 forks source link

获取页面上所有标签列表 #9

Open jiaochunxiao opened 4 years ago

jiaochunxiao commented 4 years ago

递归方式实现:

function fds (node, set = new Set()) {
  let tagName = '';
  // 
  if (node.nodeType === 1) {
    tagName = node.tagName;
    set.add(tagName); 
  }
  const children = node.childNodes;
  const len = children.length;
  for (let i = 0; i < len; i++) {
    fds(children[i], set);
  }
  return [...set];
}
fds(document);