Latest web development tutorials

HTML DOM Frame/IFrame contentDocument 屬性

Frame/IFrame 對象參考手冊 Frame/IFrame對象

定義和用法

contentDocument 屬性以HTML 對象返回框架容納的文檔。

可以通過所有標準的DOM 方法來處理被返回的對象。

注意:由於安全原因,文檔的內容只能通過同一個域名下的另外一個文檔訪問。

語法

frameObject.contentDocument

或者

iframeObject.contentDocument


瀏覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要瀏覽器都支持contentDocument 屬性

注意:如果指定了!DOCTYPE, Internet Explorer 8及更高版本支持contentDocument屬性,其他IE版本請使用contentWindow屬性。


實例

實例

通過瀏覽器實例展示瞭如何在修改iframe中文檔的背景顏色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function changeStyle(){
    var x=document.getElementById("myframe");
    var y=(x.contentWindow || x.contentDocument);
    if (y.document)y=y.document;
    y.body.style.backgroundColor="#0000ff";
}
</script>
</head>
<body>
    
<iframe id="myframe" src="demo_iframe.htm">
<p>你的浏览器不支持iframes。</p>
</iframe>
<br><br>
<input type="button" onclick="changeStyle()" value="修改背景颜色">

</body>
</html>

嘗試一下»


Frame/IFrame 對象參考手冊 Frame/IFrame對象