简体   繁体   English

使用jQuery调整iFrame高度

[英]iFrame height adjust with jQuery

I would like to auto-adjust the height of my iframe to the content that's in it. 我想将iframe的高度自动调整为其中的内容。 I've done this with a jQuery plugin, jquery-iframe-auto-height by house9, which works really well. 我使用house9的jQuery插件jquery-iframe-auto-height做到了这一点,效果非常好。

Now I want for my homepage (in the iframe) to have the full height of the parent page. 现在,我要让我的首页(在iframe中)具有父页面的完整高度。 I figured I should use an if else statement, but since I'm not that experienced with javascript (yet), I hope one of you could help me. 我认为我应该使用if else语句,但是由于我还没有使用javascript(还),因此,我希望你们中的一个可以帮助我。

$(document).ready(function () {
    if ((#ifrm).($this).attr('src') == 'home.html'){
        $('#ifrm').iframeAutoHeight({minHeight: 100%});
    }
    else $('#ifrm').iframeAutoHeight({minHeight: 300});
});

Also I found this one code for getting the full height of the page, maybe it helps 我也发现了这段代码来获取页面的全部高度,也许对您有所帮助

if ($('#ifrm').($this).attr('src') == 'home.html'){
    (function(){var height = $(window).height();
         $('#ifrm').css('height', height)
    });
    }
    else $('#ifrm').iframeAutoHeight({minHeight: 300, debug: true});

Thank you so much in advance 提前谢谢你

Edit: you can find the website this was for here 编辑:您可以在此处找到该网站

This code should work for you: 此代码应为您工作:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
    $(document).ready(function() {
        if ($('#ifrm').attr('src') == 'home.html') {
            var window_height = $(window).height();
            $('#ifrm').css("height", window_height);
        }else{
            $('#ifrm').load(function() {
                this.style.height =
                this.contentWindow.document.body.offsetHeight + 'px';
            });
        }
    });
</script>

<iframe src="home.html" id="ifrm">

It not only auto adjusts the height of the iframe to the content without using a plugin (always preferable), it also sets the full height of the "home.html" page 它不仅可以自动调整iframe的高度以适应内容,而无需使用插件(总是最好),而且还可以设置“ home.html”页面的完整高度

Bear in mind that this will only work if the iframe contents are on the same domain, the contents of iframes from other domains are locked out for security. 请记住,这仅在iframe内容位于同一域中且其他域的iframe内容出于安全性而被锁定时才有效。

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

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