我正在尝试打印来自服务器端的HTML。我现在面临的主要问题是,由于某种原因,css没有被应用。请看小提琴。我是不是做错了什么?
附注:使用带框架的JS是为了在同一选项卡中打开打印窗口。以前我遇到了麻烦,当新标签打开时,原来标签上的JS停止工作,直到我关闭第二个带有打印内容的标签
https://jsfiddle.net/7L9onps1/
@media print {
body * {
visibility: hidden;
}
.test {
visibility: visible;
position: absolute;
left: 0;
top: 0;
background: red;
color: red;
-webkit-print-color-adjust: exact;
}
}JS:
document.querySelector("#print").addEventListener("click", function() {
var html = '<div class="test">Test</div>';
print(html);
});
function print(html) {
// https://www.sitepoint.com/5-jquery-print-page-options/
document.innerHTML = html;
$('<iframe>', {
name: 'myiframe',
class: 'printFrame'
}).appendTo('body').contents().find('body').append(html);
window.frames['myiframe'].focus();
window.frames['myiframe'].print();
setTimeout(() => { $(".printFrame").remove(); }, 1000);
}发布于 2018-07-13 18:33:49
我正在使用一个库来做这个:https://github.com/DoersGuild/jQuery.print
https://stackoverflow.com/questions/51322997
复制相似问题