使用JavaScript访问框架的文档对象
Accessing the document object of a frame with JavaScript
我正在这个页面上进行测试,我不确定我缺少了什么。
// 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>
您可以使用
parent.frame.location.href = ...
其中frame是要更改的框架的名称/id。
问候Marc
相关文章:
- 如何将文档对象创建为HTML页面
- 将文档对象传递给函数
- ajax响应JavaScript中的文档对象
- XUL文档与HTML文档对象有何不同
- 我可以在不使用XMLHttpRequest或文档对象的情况下使用Javascript来获取网页吗
- 如何将外部HTML文件加载到Javascript文档对象中
- 如何将对象数组转换为单个 JSON 文档/对象
- 文档对象模型问题
- 将jQuery与我自己的文档对象一起使用
- 活动类添加到菜单中的错误文档对象
- 当我们尝试使用直接引用调用文档对象方法时,例如:var kk = document.getElementById;kk(
- 文档对象用法和行为的activeElement属性.文档对象的
- 从html页面的url获取文档对象
- clonoNode()Dom方法无法克隆给定的Xml文档对象
- Java文档对象中的Javascript
- JavaScript文档对象lastModified使用toLocale字符串
- 如何在IE9中原型扩展文档对象
- 如何从另一个框架访问框架的文档对象?
- 如何获取iframe的文档对象
- jQuery:如何取消绑定到文档对象的事件