如何动态设置iframe高度

How to Set iframe height dynamically?

本文关键字:设置 iframe 高度 动态 何动态      更新时间:2023-09-26

我有一个iframe加载src说urlA。这个urlA在内部对某个url(比如urlB)进行另一个ajax调用。

<iframe src="urlA" id="iframe1"/>

我有一个加载函数附加到上面的iframe:

$("#iframe1").load(function() {
    setIframeHeight(); // another function calls which sets iframe height according to its content
});

我的问题是,当urlA返回时,上面的加载函数得到调用,但所有操作后所需的实际内容是urlB。那么,我如何等待urlB完成并相应地设置iframe高度呢?

或者是否有任何事件在iframe内容的任何变化上被触发,以便我可以使用它来更新我的iframe的高度?

谢谢。

当Iframe内容加载时-获取其高度并相应地设置其样式:

$("iframe").load(function() {
    this.style.height = this.contentWindow.document.body.offsetHeight + "px";
});

使用jquery:

   $('#iframe1').css('height',auto);