Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

随便打开一个网页,用 JavaScript 打印所有以 s 和 h 开头的标签,并计算出标签的种类 #204

Open
lgwebdream opened this issue Jul 6, 2020 · 4 comments
Labels
JavaScript teach_tag 快手 company

Comments

@lgwebdream
Copy link
Owner

No description provided.

@lgwebdream lgwebdream added JavaScript teach_tag 快手 company labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@Carnia
Copy link

Carnia commented Feb 15, 2022

const tmp = new Set()
Array.prototype.slice.call(document.querySelectorAll("*")).forEach(v => {
    const tagName = v.tagName.toLowerCase()
    if (tagName[0] === 's' || tagName[0] === 'h') {
        tmp.add(v.tagName)
    }
})
console.log(tmp);

@mingwong1278
Copy link

二维码挂了

@gaohan1994
Copy link

const printTags = () => {
  const body = document.getElementsByTagName("body")[0];
  const tags = new Map();

  const dp = node => {
    if (node.tagName.startsWith("S") || node.tagName.startsWith("H")) {
      if (tags.has(node.tagName)) {
        const value = tags.get(node.tagName);
        value.push(node);
        tags.set(node.tagName, value);
      } else {
        tags.set(node.tagName, [node]);
      }
    }

    if (node.children.length > 0) {
      for (let index = 0; index < node.children.length; index++) {
        const child = node.children[index];
        dp(child);
      }
    }
  };

  dp(body);

  tags.forEach((nodes, key) => {
    console.log(`key: ${key}, 共: ${nodes.length} 个`);
    nodes.forEach(node => {
      console.log(node);
    });
  });
};

printTags();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 快手 company
Projects
None yet
Development

No branches or pull requests

4 participants