生活永遠是一個大染缸,一塊白布下去,黑布出來,一塊黑布下去,一塊七色布出來。
contentWindow 兼容各個瀏覽器,可取得子窗口的 window 對象。
contentDocument Firefox 支持,> ie8 的ie支持。可取得子窗口的 document 對象。
在子級iframe設置 父級 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; //設置第二層 iframe 的高度 var secondHeight = x.contentWindow.document.body.offsetHeight; //第二層 body 對象 x.height = secondHeight; //設置第一層 iframe 的高度 //alert(secondHeight); //alert('body: ' + x.contentDocument.body.offsetHeight + ' div:' + thirdHeight);}
下面附一個實例,因為2345的推廣,很多導航站都將網址直接換成2345的推廣頁面了,那就是當前域名下獲取2345中的寬度,然后通過js設置iframe內容的高度,這樣看起來是一個整體,核心內容如下
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加載的時候就觸發,完美融合。
新聞熱點
疑難解答