jQuery:Bootstrap 模式中 100% 高度的 iframe

Kar*_*Žák 3 iframe jquery bootstrap-modal bootstrap-4

我有一个 php 脚本,可以生成要打印的文档。我试图将此脚本放入 100% 宽度和高度的 bootstrap 4 模态中,以便客户端可以检查内容,然后将其打印出来。

<button type="button" id="print_modal" class="btn btn-primary" data-toggle="modal" data-target="#printmodal">Result</button>
<div class="modal fade" id="printmodal">
    <div class="modal-dialog" style="max-width:210mm !important">
        <div class="modal-content">
          <div class="modal-header">
            <button class="btn btn-primary ml-1" id="print_btn">Tisk</button>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
          </div>
          <div class="modal-body text-left" id="print_content">
            <!-- IFRAME HERE -->
          </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我首先将 iframe 放入模式中,当填充时(出于性能推理 - 有时它是一个非常长的文档) - 这是有效的

$('#print_modal').click(function () {
  $('#print_content').html('<iframe src="./index.php?print" frameborder="0" scrolling="no" id="print_frame"></iframe>');
});
Run Code Online (Sandbox Code Playgroud)

我需要在 iframe 元素上设置 100% 高度。问题是以下 jquery 脚本返回 0 高度。可能是因为默认情况下隐藏在模态中。

$("#print_frame").on("load", function () {
  $(this).height($(this).contents().find("html").height());
});
Run Code Online (Sandbox Code Playgroud)

我需要类似超时之类的东西来检查 iframe 填充后的高度,但我不知道如何做。

不工作:

在生成的 iframe 上使用 onload 参数:(返回 0px - 模态 FadeIn 可能比 iframe 附加慢...)

<iframe src="./index.php?print" onload="ifrhgh()">

function ifrhgh(){
   var iframehght =  $("#print_frame").contents().height();
}
Run Code Online (Sandbox Code Playgroud)

Kar*_*Žák 5

解决: 关键是创建iframe on shown.bs.modal(成功加载后模态触发的BS4事件)然后onload参数调用ifrhgh()函数,将iframe设置为内容

$(function () {
    $('#printmodal').on('shown.bs.modal', function () {
        $('#print_content').html('<iframe width="100%" onload="ifrhgh()" src="./index.php?print" frameborder="0" scrolling="no" id="print_frame"></iframe>');
    });
});

function ifrhgh(){
    var iframehght =  $("#print_frame").contents().height();
    $("#print_frame").height(iframehght);
}
Run Code Online (Sandbox Code Playgroud)