IFrame自适应高度

对着背影说爱祢 提交于 2020-04-07 04:54:02

  原文可以访问下面连接:http://blog.csdn.net/meizz/archive/2006/03/27/639857.aspx

代码
<script type="text/javascript" language="javascript">

        
function iframeAutoFit() {
            
try {
                
if (window != parent) {                   
                    
var frame = parent.document.getElementById("contentIframe");
                    
if (frame.contentWindow == window) {
                        
var h1 = 0, h2 = 0, d = document, dd = d.documentElement;
                        frame.parentNode.style.height 
= frame.offsetHeight + "px";
                        frame.style.height 
= "10px";

                        
if (dd && dd.scrollHeight) h1 = dd.scrollHeight;
                        
if (d.body) h2 = d.body.scrollHeight;
                        
var h = Math.max(h1, h2);

                        
if (document.all) { h += 4; }
                        
if (window.opera) { h += 1; }
                        frame.style.height 
= frame.parentNode.style.height = h + "px";
                    }

                }
            }
            
catch (ex) { }
        }
        
if (window.attachEvent) {
            window.attachEvent(
"onload", iframeAutoFit);
            
//window.attachEvent("onresize",  iframeAutoFit);
        }
        
else if (window.addEventListener) {
            window.addEventListener(
'load', iframeAutoFit, false);
            
//window.addEventListener('resize',  iframeAutoFit,  false);
        }
    
</script>


 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!