访问框架内的元素

access elements inside an frame

本文关键字:元素 框架 访问      更新时间:2023-09-26

我的html代码在下面

我需要访问b.html中的输入元素。请为我提供javascript建议。我在尝试铬控制台时出错

第一次尝试:

   document.getElementsByName['body'].contentDocument

VM331:2未捕获类型错误:无法读取未定义的属性"contentDocument"

   document.getElementsByName['body'].contentWindow.document

VM347:2未捕获类型错误:无法读取未定义的属性"contentWindow"

第二次尝试:

  var theFrame = document.getElementsByTagName("frame")[2];
  var theFrameDocument = theFrame.contentWindow.document;
  var button = theFrameDocument.getElementsByTagName("input");

VM494:3未捕获的DOMException:阻止原点为"null"的帧访问跨原点帧。

main.html

    <html>
    <frameset framepadding="0" framespacing="0" bgcolor="#F0E68C" border="0" frameborder="no">
    <frameset rows="16%,*">
    <frame bgcolor="white"  src="h.html" name="header" marginwidth="15" marginheight="40" scrolling="no" frameborder="0" noresize="" width="100%">
    <frameset bordercolor="#F0E68C" cols="200px,*">
    <frame src="l.html" name="left"/>
    <frame src="b.html" id="body1" name="body"/>
    </frameset>
    </frameset>
    <noframes>
    &amp;lt;BODY&amp;gt;
    &amp;lt;/BODY&amp;gt;
    </noframes> 
    </frameset>
    </html>

h.html

    <html>
    <body>
    header
    </body>
    </html>

l.html

    <html>
    <body>
    left
    </body>
    </html>

b.html

    <html>
    <body>
    body
    <input id="input1" value="empty" type="text" name="txtProdCoating" size="25">
    </body>
    </html>

安全限制阻止访问通过file://提供的文档中的其他文件。

运行web服务器并通过HTTP加载文档。