如何根据内容动态设置iframe高度

时间:2013-12-19 10:24:47

标签: javascript jquery iframe

我正在使用<iframe id='' src =" + url + " style='width:100%;border:none;'></iframe> 我试图根据内容高度设置iframe高度。 如果我没有提到iframe的高度,则采用默认高度。 任何人都可以告诉我如何设置iframe高度取决于内容高度....

3 个答案:

答案 0 :(得分:0)

您可以参考以下代码:

jQuery(document).ready(function(){
var height = $(window).height();
$('iframe').css('height', height)
});

在上面的示例中,我将窗口的高度设置为iframe。根据您的要求,您可以获取内容的高度,然后将其设置为iframe的高度。

希望这有帮助!

答案 1 :(得分:0)

HTML:

<iframe name="i_frame" id="i_frame" src="" onload="iframeAutoSize( this.id );">
</iframe>

JS:

function iframeAutoSize( id ) {

    iframe_element = document.getElementById( id );

    if ( iframe_element ){

        var new_height = iframe_element.contentDocument.documentElement.offsetHeight;
        //var new_width = iframe_element.contentDocument.documentElement.childNodes[2].offsetWidth;

        $("#"+id).height( new_height );
        //$("#"+id).width( new_width );
    }
}

答案 2 :(得分:0)

您可以使用以下代码。

style="height:auto;"
相关问题