简体   繁体   English

增加iframe高度的代码在Google Chrome中不起作用

[英]Code to increase the iframe height not working in Google Chrome

I have used the following code to increase the iframe height. 我使用以下代码来增加iframe高度。 This code working fine in Mozilla and IE, but not in Google Chrome. 此代码在Mozilla和IE中可以正常工作,但在Google Chrome中不能正常工作。

<script type="text/javascript">
function calcHeight()
{
    the_height = document.getElementById('the_iframe').contentWindow.document.body.scrollHeight;
    document.getElementById('the_iframe').height = the_height + 35;
}
</script>    

<iframe src="indexframe.html" id="the_iframe" onLoad="calcHeight();" name="home_frame" width="100%" frameborder="0" ></iframe>

如果您使用的是jQuery

$('#the_iframe').css({'height':(the_height+35)+'px'});

use document.documentElement.scrollHeight in other than firefox 在firefox以外的地方使用document.documentElement.scrollHeight

<iframe src="indexframe.html" id="the_iframe" onLoad="calcHeight();" name="home_frame" width="100%" frameborder="0" ></iframe>
<script type="text/javascript">
function calcHeight()
{

  the_height=document.documentElement.scrollHeight;
    document.getElementById('the_iframe').height=the_height+35;


}
</script>  

The cross-browser way is a bit more complicated 跨浏览器的方式有点复杂

<iframe src="indexframe.html" id="the_iframe" name="home_frame" width="100%" frameborder="0" ></iframe>

<script type="text/javascript">
    var iframe = document.getElementById('the_iframe');

    if (iframe.addEventListener) {
        iframe.addEventListener('load', loader, false);
    } else if (iframe.attachEvent) {
        iframe.attachEvent("onload", loader);
    } else {
        iframe.onload = loader
    }

    function loader() {
        var D = iframe.contentDocument ? iframe.contentDocument :
        (iframe.contentWindow ? iframe.contentWindow.document : iframe.document),
        the_height = Math.max(
            D.body.scrollHeight, D.documentElement.scrollHeight,
            D.body.offsetHeight, D.documentElement.offsetHeight,
            D.body.clientHeight, D.documentElement.clientHeight
        );

        iframe.height = the_height+35;
    }   
</script>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM