访问 <iframe> 的最简洁的跨浏览器方式是什么?元素的窗口和文档? [英] What's the most concise cross-browser way to access an <iframe> element's window and document?
问题描述
我正在尝试找出从父页面访问 元素的
window
和 document
属性的最佳方式.<iframe>
可以通过 JavaScript 创建或通过存储在对象属性或变量中的引用访问,因此,如果我理解正确,则排除使用 document.frames
.
I'm trying to figure out the best way to access an <iframe>
element's window
and document
properties from a parent page. The <iframe>
may be created via JavaScript or accessed via a reference stored in an object property or a variable, so, if I understand correctly, that rules out the use of document.frames
.
我已经看到有多种方法可以做到这一点,但我不确定最好的方法.给定一个以这种方式创建的 :
I've seen this done a number of ways, but I'm unsure about the best approach. Given an <iframe>
created in this way:
var iframe = document.createElement('iframe');
document.getElementsByTagName('body')[0].appendChild(iframe);
我目前正在使用它来访问 document
,它似乎可以在主要浏览器中正常工作:
I'm currently using this to access the document
, and it seems to work OK across the major browsers:
var doc = iframe.contentWindow || iframe.contentDocument;
if (doc.document) {
doc = doc.document;
}
我也看到过这种方法:
var iframe = document.getElementById('my_iframe');
iframe = (iframe.contentWindow) ? iframe.contentWindow :
(iframe.contentDocument.document) ? iframe.contentDocument.document :
iframe.contentDocument;
iframe.document.open();
iframe.document.write('Hello World!');
iframe.document.close();
这让我很困惑,因为似乎如果定义了 iframe.contentDocument.document
,您最终将尝试访问 iframe.contentDocument.document.document
.
That confuses me, since it seems that if iframe.contentDocument.document
is defined, you're going to end up trying to access iframe.contentDocument.document.document
.
还有这个:
var frame_ref = document.getElementsByTagName('iframe')[0];
var iframe_doc = frame_ref.contentWindow ? frame_ref.contentWindow.document :
frame_ref.contentDocument;
最后,我想我对哪些属性持有哪些属性感到困惑,contentDocument
是否等同于 document
或是否有这样的属性 <代码>contentDocument.document等
In the end, I guess I'm confused as to which properties hold which properties, whether contentDocument
is equivalent to document
or whether there is such a property as contentDocument.document
, etc.
任何人都可以向我指出有关这些属性的准确/及时的参考资料,或简要介绍如何有效访问 的
window
和以跨浏览器的方式document
属性(不使用 jQuery 或其他库)?
Can anyone point me to an accurate/timely reference on these properties, or give a quick briefing on how to efficiently access an <iframe>
's window
and document
properties in a cross-browser way (without the use of jQuery or other libraries)?
感谢您的帮助!
推荐答案
在此期间我做了很多测试,最后想出了这个简短但强大的语法,它适用于我可以测试的每个浏览器:
During the time I made many tests, and finally came up with this short but robust syntax which works on every browser I could test:
var doc = iframe.contentDocument ?
iframe.contentDocument : (iframe.contentWindow.document || iframe.document);
@DaggNabbit 注意到 iframe.contentWindow.document
中的引用错误,如果 iframe.contentWindow
未设置,则会阻止代码执行,不允许返回iframe.document
.
所以我改进了我的代码:
@DaggNabbit noticed that a reference error in iframe.contentWindow.document
, if iframe.contentWindow
is not set, would block the code execution, not allowing iframe.document
to be returned.
So I refined my code:
var doc = iframe.contentDocument ?
iframe.contentDocument :
(iframe.contentWindow ? iframe.contentWindow.document : iframe.document);
注意:iframe.document
是 IE5 的解决方法.
NOTE: iframe.document
is a workaround for IE5.
这篇关于访问 <iframe> 的最简洁的跨浏览器方式是什么?元素的窗口和文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!