iframe 的内容文档
对于 iframe(甚至旧的“frame”元素),“contentDocument”到底代表什么?它相当于“html”元素还是“body”元素? 它有什么用? 所有浏览器都支持此属性吗?
What exactly does "contentDocument" represent for an iframe (or even the old "frame" element)? Is it equivalent to the "html" element or the "body" element ?
What is it's use?
And is this property supported across all the browsers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
contentDocument
是获取 iframe 或框架的Document
对象的标准化方法。它与 iframe 中运行的 JavaScript 通过document
访问的对象相同。正如其他答案中所述,IE 直到版本 8 才支持它,但支持通过
contentWindow
访问 iframe 的Window
对象。因此,获取 iframe 的元素的跨浏览器方法是:
请注意,如果 iframe 不是由与主文档相同的域提供的,则浏览器安全限制将阻止访问以这种方式或任何其他方式获取其文档对象。
contentDocument
is the standardized way to get hold of the iframe or frame'sDocument
object. It is the same object as JavaScript running within the iframe would access viadocument
.As noted in other answers, IE didn't support it until version 8 but did support access to the iframe's
Window
object viacontentWindow
. A cross-browser way of getting hold of an iframe's<body>
element is therefore:Note that if the iframe is not served from the same domain as the main document, browser security restrictions will prevent access to its document object in this or any other way.
contentDocument
表示 iframe 的文档(DOM 对象)。它不等于html
因为文档有自己的属性,但是如果您输入:您将获得正文本身。
所有浏览器都支持它,只需稍作修改:对于 Internet Explorer,请使用
Enjoy、Nili
contentDocument
represents the document of an iframe (DOM object). It is not equivalent tohtml
since documents have their own properties, however if you type:You will get the body itself.
It is supported in all the browsers, with a tiny modification: for Internet Explorer use
Enjoy, Nili
w3.org
MDN
msdn
,因此要获取 body 元素的innerHTML,您可以使用
iframe.contentDocument.getElementsByTagName("body")[0]
或
iframe.contentDocument.body
在当今的浏览器中。
w3.org
MDN
msdn
So to get the innerHTML of the body element you could use
iframe.contentDocument.getElementsByTagName("body")[0]
or
iframe.contentDocument.body
in todays browsers.