生活永遠(yuǎn)是一個(gè)大染缸,一塊白布下去,黑布出來,一塊黑布下去,一塊七色布出來。
contentWindow 兼容各個(gè)瀏覽器,可取得子窗口的 window 對象。
contentDocument Firefox 支持,> ie8 的ie支持。可取得子窗口的 document 對象。
在子級iframe設(shè)置 父級 iframe ,或 孫級 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 對象 sub.height = thirdHeight; //設(shè)置第二層 iframe 的高度 var secondHeight = x.contentWindow.document.body.offsetHeight; //第二層 body 對象 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的推廣頁面了,那就是當(dāng)前域名下獲取2345中的寬度,然后通過js設(shè)置iframe內(nè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寫在前面,iframe在后面,通過onload加載的時(shí)候就觸發(fā),完美融合。
新聞熱點(diǎn)
疑難解答