我正在使用iframe来显示内容。在iframe加载和事件触发后设置iframe的高度,因为它提前知道它有多高。不幸的是,这并不总是有效。我假设这是因为在iframe中加载内容之前会触发load事件。有没有办法,在jQuery中,使这项工作正常。这是代码:
function UpdateIFrameSize(iframeSelector) {
try {
$(iframeSelector).load(function () {
$(iframeSelector).width('98%');
var height = parseInt($(iframeSelector).contents().find('body').attr('scrollHeight'));
$(iframeSelector).height(height + "px");
});
}
catch (err) {
alert(err);
}
}
答案 0 :(得分:1)
假设您正在iFrame中加载HTML页面,为什么不简单地将jQuery $(document).ready()
函数添加到iFrame内的页面,并让它使用父窗口中的委托更新高度? / p>
例如,在iFrame中:
$(document).ready(function() {
var theHeight = $(window).height();
window.parent.setiFrameHeight(theHeight);
})