深入解析contentWindow, contentDocument
生活永遠(yuǎn)是一個(gè)大染缸,一塊白布下去,黑布出來(lái),一塊黑布下去,一塊七色布出來(lái)。
contentWindow 兼容各個(gè)瀏覽器,可取得子窗口的 window 對(duì)象。
contentDocument Firefox 支持,> ie8 的ie支持??扇〉米哟翱诘?document 對(duì)象。
在子級(jí)iframe設(shè)置 父級(jí) iframe ,或 孫級(jí) iframe 高度。
function showIframeH(){
var parentWin = parent.document.getElementById("test");
if(!parentWin) return false;
var sub = parentWin.contentWindow.document.getElementById("test2");
if(!sub) return false;
var thirdHeight = sub.contentWindow.document.body.offsetHeight; //第三層 body 對(duì)象
sub.height = thirdHeight; //設(shè)置第二層 iframe 的高度
var secondHeight = x.contentWindow.document.body.offsetHeight; //第二層 body 對(duì)象
x.height = secondHeight; //設(shè)置第一層 iframe 的高度
//alert(secondHeight);
//alert('body: ' + x.contentDocument.body.offsetHeight + ' div:' + thirdHeight);
}下面附一個(gè)實(shí)例,因?yàn)?345的推廣,很多導(dǎo)航站都將網(wǎng)址直接換成2345的推廣頁(yè)面了,那就是當(dāng)前域名下獲取2345中的寬度,然后通過(guò)js設(shè)置iframe內(nèi)容的高度,這樣看起來(lái)是一個(gè)整體,核心內(nèi)容如下
iframe
<body>
<iframe id="mainFrame" onload="mainFrameLoaded(this)" scrolling="no" src="https://www.2345.com/"></iframe>
js代碼
<script>
function mainFrameLoaded(t)
{
//debugger;
//alert(t.contentWindow.document.body.offsetHeight);
t.style.height = t.contentWindow.document.body.offsetHeight + 'px';
}
</script>記住js寫(xiě)在前面,iframe在后面,通過(guò)onload加載的時(shí)候就觸發(fā),完美融合。
相關(guān)文章
詳解js運(yùn)算符單豎杠“|”與“||”的用法和作用介紹
在js開(kāi)發(fā)應(yīng)用中我們通常會(huì)碰到“|”與“||”了,那么在運(yùn)算中“|”與“||”是什么意思呢?本篇文章主要介紹了詳解js運(yùn)算符單豎杠“|”與“||”的用法,有需要的可以了解一下。2016-11-11
javascript indexOf方法、lastIndexOf 方法和substring 方法
indexOf() 方法可返回某個(gè)指定的字符串值在字符串中首次出現(xiàn)的位置。2009-03-03
BOM中l(wèi)ocation對(duì)象的屬性和方法
這篇文章主要介紹了BOM中l(wèi)ocation對(duì)象的屬性和方法,作為前端基礎(chǔ)location對(duì)象的屬性方法還是要掌握的,需要的朋友可以參考下2023-04-04
超詳細(xì)的JavaScript基本語(yǔ)法規(guī)則
這篇文章主要介紹了JavaScript基本語(yǔ)法規(guī)則,保姆級(jí)的詳細(xì)教程,萬(wàn)字長(zhǎng)文詳細(xì)的列出了JavaScript的各種語(yǔ)法,建議收藏系列,希望可以有所幫助2021-08-08

