日期:2014-05-17  浏览次数:20476 次

哪位老板手上有iframe[高度和宽度]自适应的相关代码
哪位老板有iframe[高度和宽度]自适应的相关代码?img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/10.gif][/img]
以下这段代码是我现在正在用的,在IE7,IE8下面没问题.但是在其他浏览器下就不行了.想找一个不求完美但求兼容性稍好点儿的.网上我都找得差不多了,就那几种,各位大神手上有没有存货啊!
<script type="text/javascript" language="javascript">  
<!--  
function TuneHeight(fm_name,fm_id){  
    var frm=document.getElementByIdx(fm_id);  
    var subWeb=document.frames?document.frames[fm_name].document:frm.contentDocument;  
    if(frm != null && subWeb != null){
        frm.style.height = subWeb.documentElement.scrollHeight+"px";
//如需自适应宽高,去除下行的“//”注释即可
//      frm.style.width = subWeb.documentElement.scrollWidth+"px";
    }  
}  
//-->  
</script>  
<iframe
onLoad="{TuneHeight('FM_Name','FM_Id');}"
src="http://www.oyksoft.com" name="FM_Name"
id="FM_Id" width="800" marginwidth="0"
marginheight="0"
frameborder="0" scrolling="no">
</iframe>

------解决方案--------------------
function initIframe(obj) {
        try {
            var bHeight = obj.contentWindow.document.body.scrollHeight;
            var dHeight = obj.contentWindow.document.documentElement.scrollHeight;
            var height = Math.max(bHeight, dHeight);
            obj.height = height;
        } catch (ex) { }
}
<iframe frameborder="0" width="100%" src="demo.html" id="IFrame" scrolling="no" onload="initIframe(this);"></iframe>