为什么打印功能在火狐浏览器上不起作用



Bonjour à tous

我正在尝试在javascript/jquery中创建一个打印函数。我的代码在chrome和Internet Explorer上运行得很好。另一方面,它在火狐上根本不起作用。

我确实有最新的火狐更新。我测试了互联网上随处可见的不同解决方案(例如使用 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;
}

最新更新