Frame/IFrame contentWindow 屬性

定義和用法
contentDocument 屬性能夠以 HTML 對象來返回 iframe 中的文檔。
可以通過所有標準的 DOM 方法來處理被返回的對象。
語法
frameObject.contentWindow
或者
iframeObject.contentWindow
或者
iframeObject.contentWindow
流覽器支持
所有主要流覽器都支持 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>
