简体   繁体   English

HTML iFrame内容

[英]HTML iFrame contents

When I access the iframe with a=window.frames["iframe"] it says that a is a DOMWindow. 当我使用a = window.frames [“ iframe”]访问iframe时,它说a是DOMWindow。
But when I try to access document it says undefined, but when I use top it refers back to it self. 但是,当我尝试访问文档时,它说未定义,但是当我使用top时,它指的是它自己。 It's like the iframe only have itself as a property. 就像iframe仅具有自身的属性一样。
When i access it trough document.getElementById("iframe") it identifies itself as HTMLIFrame, but the problem still exists, this is really weird and i need help. 当我通过document.getElementById(“ iframe”)访问它时,它会将自己标识为HTMLIFrame,但是问题仍然存在,这确实很奇怪,我需要帮助。

I have tried: 我努力了:

document.getElementById("iframe").getDocument
document.getElementById("iframe").getDocument()
document.getElementById("iframe").html.body
document.getElementById("iframe").body
document.getElementById("iframe").document.getElementById("btag")

and many more combinations. 以及更多其他组合。

I seriously do not know why it acts this way. 我真的不知道为什么会这样。

Thanks. 谢谢。

You need to access either the contentWindow or contentDocument depending on the browser. 您需要根据浏览器访问contentWindowcontentDocument

I've done some light testing and it seems .document isn't working (I'm in chrome) this may be a better solution: 我已经进行了一些轻度测试,似乎.document无法正常工作(我使用的是chrome),这可能是一个更好的解决方案:

http://jsfiddle.net/sTkVR/4/ http://jsfiddle.net/sTkVR/4/

document.frames['frmID'] returns the equivalent as contentWindow or contentDocument which is why it says DOMWindow document.frames ['frmID']返回与contentWindowcontentDocument等价的contentDocument ,这就是为什么说DOMWindow

The window.frames property is actually a circular reference to the window itself. window.frames属性实际上是对窗口本身的循环引用。

window.frames === window
window.frames === window.self

DOMWindow is an Array-like structure in that it has a length property and properties of numerical keys. DOMWindow是类似Array的结构,它具有length属性和数字键属性。

The window.frames property exists for the sole reason of providing access to frames by their numerical indexes such as window.frames[0] or just frames[0] . 存在window.frames属性的唯一原因是通过它们的数字索引(例如window.frames[0]或仅仅是frames[0]提供对框架的访问。 You could, of course, access the given frame as window[0] but that just does not look correct. 您当然可以以window[0]访问给定的帧,但这看起来并不正确。

Elements of this pseudo-Array (accessed as frames[index] ) are instances of DOMWindow (in case of iframes, they point to iframe.contentWindow ) and you can access their document properties if they have a common origin with the current window (ie same protocols, domains and ports). 该伪数组的元素(以frames[index]访问)是DOMWindow实例(在iframe的情况下,它们指向iframe.contentWindow ),并且如果它们与当前窗口具有相同的来源,则可以访问其document属性。相同的协议,域和端口)。

If you name a frame or an iframe (with an id attribute), they are (at least in WebKit-based browsers) mapped to properties of their owner window. 如果命名框架或iframe(具有id属性),它们(至少在基于WebKit的浏览器中)将映射到其所有者窗口的属性。 In case of an iframe , the property points to the <iframe> element rather than to its contentWindow property. 对于iframe ,该属性指向<iframe>元素,而不是其contentWindow属性。

// <iframe name="a">
frames['a'] === window['a'] === document.getElementById('a')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM