使用JavaScript访问框架的文档对象 [英] Accessing the document object of a frame with JavaScript

查看:103
本文介绍了使用JavaScript访问框架的文档对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在此页面进行测试,而我不确定我错过了什么。

I'm testing on this page, and I'm not sure what I'm missing.

// 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(最新的稳定版)中测试。

It seems like this should work, so what's the problem? It needs to work in IE8, but I'm also testing in Chrome (newest stable).

推荐答案

全面的方式获取框架的内容是这样的:

The all-around way to getting a frame's contents is with something like this:

var theFrame = document.getElementsByTagName("frame")[0];
var theFrameDocument = theFrame.contentDocument || theFrame.contentWindow.document;
var button = theFrameDocument.getElementById("mybutton");

但是,可以获得< frame> 的文档使用其名称,如:

However, it is possible to get a <frame>'s document by using its name, like:

window.frames["frame_name"].document

如果HTML是:

<frame name="frame_name">...</frame>

这篇关于使用JavaScript访问框架的文档对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆