【问题标题】:getting iframe height inside jQuery tabs returns 0在 jQuery 选项卡中获取 iframe 高度返回 0
【发布时间】:2015-03-20 02:24:57
【问题描述】:

我在 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);
}

【问题讨论】:

  • 相关标签页在页面加载时是否处于活动状态?如果没有,您将需要使用选项卡机制的回调来获取 iframe 高度可见时。
  • 就像上面的评论... iframe 需要实际可见(即不是显示:无或在具有显示的容器中:无)。
  • 它不在活动标签中

标签: jquery iframe tabs height


【解决方案1】:

一旦标签被加载,你可以使用 jQueryUI 的回调来获取 iframe 的高度:

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

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

【讨论】:

  • 加载无效,但激活有效,请编辑您的答案,以便我接受。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 2019-11-18
  • 1970-01-01
  • 2010-12-05
  • 2013-01-26
  • 2016-07-11
相关资源
最近更新 更多