使用JavaScript访问框架的文档对象

时间:2022-04-17 15:55:59

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

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).

看起来这应该有用,那么问题是什么?它需要在IE8中工作,但我也在Chrome中测试(最新的稳定版)。

2 个解决方案

#1


25  

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");

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

但是,可以使用其名称来获取的文档,例如:

window.frames["frame_name"].document

if the HTML were:

如果HTML是:

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

#2


0  

You could use

你可以用

parent.frame.location.href = ...

Where frame is the name/id of the frame you d like to change.

其中frame是您想要更改的框架的名称/ ID。

Greets Marc

#1


25  

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");

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

但是,可以使用其名称来获取的文档,例如:

window.frames["frame_name"].document

if the HTML were:

如果HTML是:

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

#2


0  

You could use

你可以用

parent.frame.location.href = ...

Where frame is the name/id of the frame you d like to change.

其中frame是您想要更改的框架的名称/ ID。

Greets Marc

相关文章