function customPrint(content, option = {}) {
const removeOldIframe = () => {
const oldPrintIframe = document.getElementById('printIframe');
oldPrintIframe && oldPrintIframe.remove && oldPrintIframe.remove();
}
removeOldIframe();
const printIframe = document.createElement('iframe');
printIframe.id = 'printIframe';
document.body.appendChild(printIframe);
const printIframeWindow = printIframe.contentWindow;
printIframeWindow.document.title = option.title || "";
printIframeWindow.document.write(content);
printIframeWindow.addEventListener('beforeprint', () => {
option.beforePrint && option.beforePrint(printIframe);
})
printIframeWindow.addEventListener('afterprint', () => {
option.afterPrint && option.afterPrint(printIframe);
removeOldIframe();
})
setTimeout(() => {
printIframeWindow.focus();
printIframeWindow.print();
})
}
调用方法customPrint(res.data);
标签:printIframeWindow,const,option,自定义,js,printIframe,新窗口,oldPrintIframe,document From: https://www.cnblogs.com/Agreoself/p/16876502.html