Frame/IFrame contentDocument 屬性

定義和用法
contentDocument 屬性以 HTML 對象返回框架容納的文檔。
可以通過所有標準的 DOM 方法來處理被返回的對象。
注意:由於安全原因,文檔的內容只能通過同一個功能變數名稱下的另外一個文檔訪問。
語法
frameObject.contentDocument
或者
iframeObject.contentDocument
或者
iframeObject.contentDocument
流覽器支持
所有主要流覽器都支持 contentDocument 屬性
注意:如果指定了 !DOCTYPE, Internet Explorer 8 及更高版本支持 contentDocument 屬性,其他IE版本請使用 contentWindow 屬性。
實例
實例
通過流覽器實例展示了如何在修改iframe中文檔的背景顏色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.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>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.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>
