1

我需要在页面中包含一个 iframe,并且我想计算正确的高度并对其进行调整以避免显示垂直滚动条。不过,如果需要,我想显示水平滚动条。

我发现了几篇关于这个问题的帖子,但没有人能 100% 工作。这是我见过的一种非常常见的方法:

$(document).ready(function() {      
$('iframe').load(function() {
$("#your-iframe-id").height($("#your-iframe-id").contents().find("html").height()+35);
});
})

<iframe id="your-iframe-id" style="width:100%;border:0;" src="test.php"></iframe>

我发现这种方法的问题是,对于 Safari 和 Chrome,如果导航到 iframe,我访问的页面比前一个页面短,那么框架的高度仍然是较高页面的高度。相反,如果您从短页面导航到高页面,则一切正常。Firefox 没有这个问题;我没有尝试使用 IE 和 Opera。

非常感谢任何帮助。提前致谢。

4

1 回答 1

1
<script>
    var resizeHandle = function()
    {
        var theFrame = $('.window-content-wrapper', parent.document.body);
        theFrame.height($(document).height());      
        var theFrame = $('.window-content', parent.document.body);
        theFrame.height($(document).height());
    };
</script>

<iframe id ="home"  onResize="resizeHandle()" onload="resizeHandle()" style="width:100%;border:0;" src="test.php"></iframe>
于 2012-12-28T17:35:12.453 回答