首页 文章

获取jQuery标签内的iframe高度返回0

提问于
浏览
1

我在jQuery选项卡中有多个iframe,它们不在活动选项卡中 . 每个标签都包含一个iframe . 我正在尝试获取iframe内容的高度以扩展iframe高度 . 问题是,如果iframe位于选项卡内,则函数提醒0,但如果iframe直接位于页面主体中,则工作正常 . 这是iframe代码

<div id="discussion">
    <iframe src="page path here" id="myFrame" style="width: 90%; 
     float: right;border: 0;" onload="calcHeight()"></iframe>
</div>

和功能

function calcHeight(){
    var the_height=document.getElementById('myFrame')
        .contentWindow.document.body.scrollHeight;
    document.getElementById('myFrame').height=the_height;
    $("#myFrame").css("height",the_height);
    alert(the_height);
}

1 回答

  • 1

    加载选项卡后,您可以使用jQueryUI的回调来获取iframe高度:

    $( ".selector" ).tabs({
        activate: function( event, ui ) {
            calcHeight();
        }
    });
    

    http://api.jqueryui.com/tabs/#event-activate

相关问题