如何调用onclick打印函数,然后忘记它

How to call a onclick print function and forget it

本文关键字:函数 然后 忘记 打印 onclick 何调用 调用      更新时间:2023-09-26

我写了一个打印函数,它为打印打开了一个新的选项卡窗口,然后打印按钮保持活跃,直到我没有通过取消或打印关闭该窗口。在此期间,我无法在父页面上调用我的其他javascript函数。所以我的问题是,有没有可能打开一个打印窗口,打印按钮再次变得不活动,窗口保持打开打印?下面是我的函数。

function g_print_div(in_div_id) {
    div_id = in_div_id || 'idprint';
    var divElements = document.getElementById(div_id).innerHTML;
    var oldPage = document.body.innerHTML;
    var printWindow = window.open();
    printWindow.document.write('<html>');
    printWindow.document.write("<link rel='stylesheet' type='text/css' href='/static/css/print.css') %]' />");
    printWindow.document.write('<body >');
    printWindow.document.write(divElements);
    printWindow.document.write('</body></html>');
    printWindow.print();
    printWindow.document.close();
    printWindow.close();
}

调用onclick='g_print_div();'函数,如果有其他方法可以实现,请告诉我。

我搜索了很多,并找到了其他解决方案,我在相同的选项卡窗口打开打印,而不是在其他窗口或新选项卡。下面的链接对此很有帮助http://www.aspsnippets.com/Articles/Print-DIV-content-without-opening-new-popup-window-using-JavaScript.aspx