`

iframe自适应高度(兼容多种浏览器)

 
阅读更多

    最近些项目遇到用iframe的地方,发现设置的固定高有时不能完全适应项目环境,不是高了就是不够高,在页面里看着很不爽。

       想来想去,何不让iframe自适应高度呢。经过一番折腾,最终还是弄出来。下面是实现的源码:

<div id="leamain">

       <iframe src="#" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="765" height=100% id="iframepage" name="iframepage" onLoad="iFrameHeight()" ></iframe>

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

    function iFrameHeight() {

        var ifm= document.getElementById("iframepage");

        var subWeb = document.frames ? document.frames["iframepage"].document :

ifm.contentDocument;

            if(ifm != null && subWeb != null) {

            ifm.height = subWeb.body.scrollHeight;

            }

    }

</script> 

     </div>

 

以上亲测,ie、火狐、谷歌等浏览器均没问题,希望对你有用。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics