当我尝试打印浮动图表时,我遇到了同样的问题。打开新窗口的另一种方法是将画布移动到页面顶部并隐藏其他所有内容,打印,然后将所有内容放回原处。这应该适用于现代浏览器以及 IE8。此外,这将使您的外部样式和库(jquery/flot)保持引用,因为它在同一页面上。
使用html结构:
<body>
<div id="wrapper">
<div id="some-element></div>
<canvas id="my-canvas"></canvas>
<button type="button" id="print-button">PRINT</button>
<div id="some-other-element></div>
</div>
</body>
和javascript函数:
function printContent(whatToPrint, priorSibling, afterSibling)
{
$('#wrapper').hide();//hide content wrapper inside of body
//take what to print out of wrapper and place directly inside body
$(whatToPrint).appendTo('body');
window.print();//print the window
//put everything back
$('#wrapper').show();
if (priorSibling != null)
{
$(whatToPrint).insertAfter(priorSibling);
}
else if (afterSibling != null)
{
$(whatToPrint).insertBefore(afterSibling);
}
}
和 javascript 打印按钮事件
$('#print-button').click(function(){
printContent($('#my-canvas'), $('#some-element'), null);
});
注意:在不使用系统对话框打印的情况下以 chrome 打印,可能会弄乱您的样式。这只是铬。如果有人对此有解决方案,请告诉我。