Frame/IFrame contentDocument 屬性

定義和用法
contentDocument 屬性以 HTML 對(duì)象返回框架容納的文檔。
可以通過所有標(biāo)準(zhǔn)的 DOM 方法來處理被返回的對(duì)象。
注意:由于安全原因,文檔的內(nèi)容只能通過同一個(gè)域名下的另外一個(gè)文檔訪問。
語法
frameObject.contentDocument
或者
iframeObject.contentDocument
或者
iframeObject.contentDocument
瀏覽器支持
所有主要瀏覽器都支持 contentDocument 屬性
注意:如果指定了 !DOCTYPE, Internet Explorer 8 及更高版本支持 contentDocument 屬性,其他IE版本請(qǐng)使用 contentWindow 屬性。
實(shí)例
實(shí)例
通過瀏覽器實(shí)例展示了如何在修改iframe中文檔的背景顏色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool在線教程(w3cschool.cn)</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="/upload/help/demo_iframe.htm">
<p>你的瀏覽器不支持iframes。</p>
</iframe>
<br><br>
<input type="button" onclick="changeStyle()" value="修改背景顏色">
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool在線教程(w3cschool.cn)</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="/upload/help/demo_iframe.htm">
<p>你的瀏覽器不支持iframes。</p>
</iframe>
<br><br>
<input type="button" onclick="changeStyle()" value="修改背景顏色">
</body>
</html>
嘗試一下 ?
