Bri*_*and 12 html javascript dom frame
我在这个页面上测试,我不确定我缺少什么.
// 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
Run Code Online (Sandbox Code Playgroud)
看起来这应该有效,所以问题是什么?它需要在IE8中工作,但我也在Chrome中测试(最新的稳定版).
Ian*_*Ian 31
获取框架内容的全方位方式是这样的:
var theFrame = document.getElementsByTagName("frame")[0];
var theFrameDocument = theFrame.contentDocument || theFrame.contentWindow.document;
var button = theFrameDocument.getElementById("mybutton");
Run Code Online (Sandbox Code Playgroud)
但是,可以<frame>
通过使用其名称来获取文档,例如:
window.frames["frame_name"].document
Run Code Online (Sandbox Code Playgroud)
如果HTML是:
<frame name="frame_name">...</frame>
Run Code Online (Sandbox Code Playgroud)