Javascript iframe 的 contentDocument

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6581803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:21:57  来源:igfitidea点击:

contentDocument for an iframe

javascripthtmldomiframeframes

提问by testndtv

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?

“contentDocument”对于 iframe(甚至旧的“frame”元素)究竟代表什么?它相当于“html”元素还是“body”元素?它有什么用?所有浏览器都支持此属性吗?

回答by mplungjan

w3.org

w3.org

contentDocument of type Document, readonly, introduced in DOM Level 2 The document this frame contains, if there is any and it is available, or null otherwise.

contentDocument 类型 Document,只读,在 DOM Level 2 中引入 该框架包含的文档,如果有并且可用,否则为 null。

MDN

MDN

From the DOM iframe element, scripts can get access to the window object of the included HTML page via the contentWindow property. The contentDocument property refers to the document element inside the iframe (this is equivalent to contentWindow.document), but is not supported by Internet Explorer versions before IE8.

从 DOM iframe 元素,脚本可以通过 contentWindow 属性访问包含的 HTML 页面的 window 对象。contentDocument 属性指的是 iframe 内的文档元素(这相当于 contentWindow.document),但 IE8 之前的 Internet Explorer 版本不支持。

msdn

msdn

the document this page or frame contains
This property is new in Windows Internet Explorer 8

此页面或框架包含的文档
此属性是 Windows Internet Explorer 8 中的新属性

So to get the innerHTML of the body element you could use

因此,要获取您可以使用的 body 元素的 innerHTML

iframe.contentDocument.getElementsByTagName("body")[0]

iframe.contentDocument.getElementsByTagName("body")[0]

or

或者

iframe.contentDocument.body

iframe.contentDocument.body

in todays browsers.

在今天的浏览器中。

回答by Tim Down

contentDocumentis the standardized way to get hold of the iframe or frame's Documentobject. It is the same object as JavaScript running within the iframe would access via document.

contentDocument是获取 iframe 或框架Document对象的标准化方法。它与在 iframe 中运行的 JavaScript 将通过document.

As noted in other answers, IE didn't support it until version 8 but did support access to the iframe's Windowobject via contentWindow. A cross-browser way of getting hold of an iframe's <body>element is therefore:

正如其他答案中所述,IE 直到版本 8 才支持它,但确实支持Window通过contentWindow. <body>因此,获取 iframe元素的跨浏览器方式是:

var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
var iframeBody = iframeDoc.body;

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.

请注意,如果 iframe 不是来自与主文档相同的域,浏览器安全限制将阻止以这种方式或任何其他方式访问其文档对象。

回答by Nili

contentDocumentrepresents the document of an iframe (DOM object). It is not equivalent to htmlsince documents have their own properties, however if you type:

contentDocument表示 iframe(DOM 对象)的文档。它不等同于html因为文档有自己的属性,但是如果您键入:

myFrame.contentDocument.body 

You will get the body itself.

你会得到身体本身。

It is supported in all the browsers, with a tiny modification: for Internet Explorer use

所有浏览器都支持它,稍作修改:供 Internet Explorer 使用

myFrame.contentWindow.document

Enjoy, Nili

享受,妮莉