繁体   English   中英

为什么打印功能在 Firefox 上不起作用

[英]Why print function doesn't work on Firefox

Bonjour à tous

我正在尝试在 javascript/jquery 中创建一个打印功能。 我的代码在 chrome 和 Internet Explorer 上运行良好。 另一方面,它在 Firefox 上根本不起作用。

我确实有最新的 Firefox 更新。 我测试了在互联网上随处可见的不同解决方案(例如使用 setTimeout 等)。 但它不起作用,无法在Firefox上打开打印弹出窗口进行打印。

function printDiv() {
  let str = $('#printableTable').html();
  window.frames["print_frame"].document.body.innerHTML = str;
  window.frames["print_frame"].window.focus();
  window.frames["print_frame"].window.print();
}

$('.printDiv').click(function() {
  printDiv();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="printableTable">

  <h1 id="cnpePrint" style="text-align: center;"></h1>
  <h2 id="servicePrint" style="text-align: center;"></h2>

  <table style="border-collapse:collapse;border: 1px solid black;text-align:center;margin:auto;">
    <thead>
      <tr style="border: 1px solid black;">
        <th style="border: 1px solid black;width:100px;">Nom</th>
        <th style="border: 1px solid black;width:100px;">Prénom</th>
        <th style="border: 1px solid black;width:100px;">N° court</th>
      </tr>
    </thead>
    <tbody id="listPrint">

    </tbody>
  </table>
  <iframe class="print_frame" name="print_frame" width="0" height="0" frameborder="0" src="about:blank"></iframe>
</div>
<button class="printDiv">Print</button>

我有点难以理解为什么 Firefox 会阻止预期的事件。

感谢您的帮助

最后,解决方案是制作另一个脚本。 它仅适用于 Firefox 和 Explorer。 但在 Chrome 上并不完全好,但是,我的客户没有 Chrome,所以没关系。

function printDiv() {

    let str = $('#printableTable').html();
    let mywindow = window.open('', 'PRINT', 'height=400,width=600');
    mywindow.document.write(str);

    mywindow.document.close();
    mywindow.focus();

    mywindow.print();
    mywindow.close();

    return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM