我正在做一个元素,我想打印 div 内容。我使用的代码满足了我的要求,但我得到的是简单的输出,没有应用 Css,也没有得到图像。请帮助我。我附上了我得到的代码和输出以及我想要的输出。
代码:
function PrintElem(elem) {
Popup($(elem).html());
}
function Popup(data) {
var mywindow = window.open('', 'new div', 'height=400,width=600');
mywindow.document.write('<html><head><title></title>');
mywindow.document.write('<link rel="stylesheet" href="css/midday_receipt.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.print();
mywindow.close();
return true;
}
我在点击打印按钮之前得到的输出:
点击打印按钮得到的输出:
最佳答案
如果你想呈现 CSS,你必须在打印之前添加一个超时,因为 CSS 需要一些时间才能应用到 HTML,然后你才能打印它。
试试这个:
function Popup(data) {
var mywindow = window.open('', 'new div', 'height=400,width=600');
mywindow.document.write('<html><head><title></title>');
mywindow.document.write('<link rel="stylesheet" href="css/midday_receipt.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close();
mywindow.focus();
setTimeout(function(){mywindow.print();},1000);
mywindow.close();
return true;
}
关于javascript - 使用 css 打印 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21379605/