我正在测试 this page ,我不确定我错过了什么。
// Two frames on the page
> document.getElementsByTagName("frame").length
2
// Same domain, so no security restrictions
> document.getElementsByTagName("frame")[0].src
"http://www.quackit.com/html/templates/frames/menu_1.html"
> window.location.href
"http://www.quackit.com/html/templates/frames/frames_example_1.html"
// Can't access the document
> document.getElementsByTagName("frame")[0].document
undefined
这看起来应该可行,那么问题是什么?它需要在 IE8 中运行,但我也在 Chrome(最新稳定版)中进行测试。
最佳答案
获取框架内容的全面方法是使用如下内容:
var theFrame = document.getElementsByTagName("frame")[0];
var theFrameDocument = theFrame.contentDocument || theFrame.contentWindow.document;
var button = theFrameDocument.getElementById("mybutton");
但是,有可能得到 <frame>
使用其名称的文档,例如:
window.frames["frame_name"].document
如果 HTML 是:
<frame name="frame_name">...</frame>
关于javascript - 使用 JavaScript 访问框架的文档对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38393706/