为什么document.getElementById在页面中获取多个iframe时返回null

ted*_*eum 3 html javascript iframe

我试试这个页面,只有if1返回,if2和if3返回null,为什么?下面是html代码谢谢.(小提琴)

     <html>
 <head>
  <title> New Document </title>
  <script type="text/javascript">
  <!--
    function dotest() {
        alert(document.getElementById("if1"));
        alert(document.getElementById("if2"));
        alert(document.getElementById("if3"));
    }
  //-->
  </script>
 </head>
 <body>
 <input type="button" value="Test Test" onclick="dotest()">
  <iframe id="if1"/>
  <iframe id="if2"/>
  <iframe id="if3"/>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

JJJ*_*JJJ 6

您无法自行关闭iframe代码.将它们改为:

<iframe id="if1"></iframe>
<iframe id="if2"></iframe>
<iframe id="if3"></iframe>
Run Code Online (Sandbox Code Playgroud)

会发生什么情况是浏览器认为第一个iframe未关闭,因此它会在文档末尾隐式关闭它,并且标记内的任何内容都被视为不支持iframe的浏览器的后备.