IFrame contentDocument 属性

定义和用法

contentDocument 属性返回由 frame 或 iframe 元素生成的 Document 对象

此属性可在宿主窗口中用于访问属于框架或 iframe 元素的 Document 对象

注意:出于安全原因,只有当两个文档位于同一域中时,才能从另一个文档访问文档的内容。

实例

例子 1

关于如何更改 iframe 中包含的文档的背景颜色的跨浏览器实例:

var x = document.getElementById("myframe");
var y = (x.contentWindow || x.contentDocument);
if (y.document)y = y.document;
y.body.style.backgroundColor = "red";

Prueba por ti mismo

例子 2

如何访问 iframe 的文档以更改背景颜色的另一个例子:

var x = document.getElementById("myframe");
var y = x.contentDocument;
y.body.style.backgroundColor = "red";

Prueba por ti mismo

Sintaxis

iframeObject.contentDocument

Detalles técnicos

Valor de retorno: Referencia al objeto de documento. Si no hay documento, el valor de retorno es null.

Compatibilidad con navegadores

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Soporte Soporte Soporte Soporte Soporte