iframe 自適應高度,更改內容時iframe高度怎麼根據新的內容高度自動改變
阿新 • • 發佈:2019-01-08
//code from http://caibaojian.com/iframe-adjust-content-height.html <iframe src="backtop.html" frameborder="0" scrolling="no" id="test" onload="this.height=100"></iframe> <script type="text/javascript"> function reinitIframe(){ var iframe = document.getElementById("test"); iframe.height = 0; //只有先設定原來的iframe高度為0,之前的iframe高度才不會對現在的設定有影響 try{ var bHeight = iframe.contentWindow.document.body.scrollHeight; var dHeight = iframe.contentWindow.document.documentElement.scrollHeight; var height = Math.max(bHeight, dHeight); iframe.height = height; console.log(height); }catch (ex){} } window.setInterval("reinitIframe()", 200); </script>