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

查看:22
本文介绍了使用 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");

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

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天全站免登陆