iframe自适应高度

来源:互联网 发布:免流软件下载 编辑:程序博客网 时间:2024/05/18 00:15
一、父页面
<script type="text/javascript">
function setIframeHeight(iframe) {
    if (iframe) {
        var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
        if (iframeWin.document.body) {
            iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
        }
    }
};
</script>

使用iframe 并添加onload事件
<iframe name="issues" id="issues" src="demo.html" width="100%" onload="setIframeHeight(this)"></iframe>


二、子页面(IFrame的具体页面)

<script type="text/javascript">
function iFrameResize() {
    var obj = parent.document.getElementById("issues"); //取得父页面IFrame对象
    //alert(obj.height); //弹出父页面中IFrame中设置的高度
    obj.height = this.document.body.scrollHeight;
};
</script>

body体添加onload事件
<body onload="iFrameResize()">



参考:http://www.sharejs.com/jquery/plugin/10927
0 0