Resize iframe to content with Jquery

前端 未结 6 2188
孤街浪徒
孤街浪徒 2020-12-03 18:08

I\'m trying to resize an iframe dynamicly to fit its content. To do so I have a piece of code:

$(\"#IframeId\").height($(\"#IframeId\").contents().find(\"htm         


        
6条回答
  •  借酒劲吻你
    2020-12-03 18:52

    You just need to apply your code on the iframe load event, so the height is already known at that time, code follows:

    $("#IframeId").load(function() {
        $(this).height( $(this).contents().find("body").height() );
    });
    

    See working demo . This demo works on jsfiddle as I've set the iframe url to a url in the same domain as the jsfiddle result iframe, that is, the fiddle.jshell.net domain.

    UPDATE:
    @Youss: It seems your page for a strange reason don't get the body height right, so try using the height of the main elements instead, like this:

    $(document).ready(function() {
        $("#IframeId").load(function() {
                var h = $(this).contents().find("ul.jq-text").height();
                h += $(this).contents().find("#form1").height();
                $(this).height( h );
            });
    });
    

提交回复
热议问题