在 Chrome 开发控制台中显示重复的 ID

Ada*_*ers 6 html javascript google-chrome-devtools

有没有办法让 Chrome 开发控制台就页面上的重复 ID 发出警告?我无法告诉你有多少次我被一个问题难住了,最后才意识到 2 个元素共享相同的 id 并搞乱了代码。有没有人对如何捕获类似这样的事情有任何其他建议,而 chrome 默认情况下似乎没有警告?

bet*_*Joe 13

在 devtools 中运行它会将它们打印出来

const dupes = Object.entries(
    [...document.querySelectorAll("[id]")]
      .map((x) => x.id) /* get all ids */
      .reduce(
        (acc, id) => ({ ...acc, [id]: (acc[id] || 0) + 1 }),
        {}
      ) /*count them*/
  ).filter(([_key, val]) => val > 1); /* find the ones repeating more than once */
  
console.warn(dupes);
Run Code Online (Sandbox Code Playgroud)


Spe*_*ric 3

Chrome 的开发者控制台不提供执行此操作的功能。

不过,有一个Chrome 扩展程序可以为您做到这一点。