`id` 属性是否需要在 iframe 中唯一?

Mac*_*ack 5 html iframe

例如,这是有效的吗?

<!doctype html>
<html>

  <head>
    <title>Some Iframes</title>
  </head>

  <body>
    <iframe id="frame1" src="/html/test-frame.html"></iframe>
    <iframe id="frame2" src="/html/test-frame.html"></iframe>
  </body>

</html>
Run Code Online (Sandbox Code Playgroud)

该文件test-frame.html包含以下内容:

<!doctype html>
<html>

  <head>
    <title>Test Iframe</title>
  </head>

  <body>
    <button id="subscribe">Subscribe</button>
  </body>

 </html>
Run Code Online (Sandbox Code Playgroud)

我希望能够对一组 iframe 使用相同的 html 和相同的脚本。

Bol*_*ock 4

这是可以接受的并且有效的。由于每个页面都是独立的,因此拥有自己的命名空间和 DOM,因此在 iframe 之间使用相同的 ID 并没有什么坏处,只要它在每个 iframe 文档中只出现一次即可。