根据内容设置 iFrame 宽度和高度

Set iFrame width and height based on content

本文关键字:高度 iFrame 设置      更新时间:2023-09-26

目前我有一个jquery iframe模式对话框,可以打开一个ASPX页面。此页面包含另一个带有jQuery的iFrame。 内容 iframe 没有设置任何高度或宽度,因此根据内部内容的大小调整大小。 外部 jQuery iframe 模态不会根据其中的内容 iframe 调整大小。这是打开模态的代码:

function openModal() {
    var horizontalPadding = 30;
    var verticalPadding = 30;
    var jqurl = "ContentPage.aspx";
    var title = "Modal Title";
    jQuery('<iframe scrolling="no" id="cLaunch" class="cLaunch" src="' + jqurl + '" frameborder="0"/>').dialog({
        title: title,
        modal: true,
        resizable: true,
        position: ['center', 50],
        fluid:true,
        overlay: {
            opacity: 0.5,
            background: "grey"
        }
    });
}
setInterval(function () {
    document.getElementById("cLaunch").style.width = document.getElementById("cLaunch").contentWindow.document.documentElement.scrollWidth + 30 +  'px';
    document.getElementById("cLaunch").style.height = document.getElementById("cLaunch").contentWindow.document.body.scrollHeight + 'px';

}, 1000)

连接页面.aspx:

<iframe name="contentloader"  id="contentloader"  frameborder="0" scrolling="no"></iframe>

任何帮助将不胜感激。

尝试根据 iframe 内加载的内容设置 iframe 的宽度和高度

$("#cLaunch").load(function() {
    $(this).height( $(this).contents().find("body").height());
    $(this).width( $(this).contents().find("body").width());
});