在IE8中打印带背景颜色的div内容

时间:2017-05-18 16:07:28

标签: jquery html css canvas internet-explorer-8

我正在尝试使用jquery打印div。 (window.print())。 div有背景色。当我尝试打印时,它不会打印背景颜色。有什么办法吗?

在IE9及以上版本中,我设法使用html2canvas将其转换为图像uri,但这在IE8上无效。

如果有任何方法请告诉我。感谢

1 个答案:

答案 0 :(得分:0)

这个没有完美的解决方案,但您可以使用@media print为您的打印添加样式,然后为div

定义所需的样式

enter image description here

-webkit-print-color-adjust: exact;仅适用于像chrome这样的webkit broswers。参考:https://developer.mozilla.org/en/docs/Web/CSS/-webkit-print-color-adjust

function myFunction() {
  background: window.print();
}
@media print {
  div {
    background-color: red;
    -webkit-print-color-adjust: exact;
  }
}

div {
  background-color: red;
}
<div>I'm the div and I should be red</div>

<button onclick="myFunction()">Print this page</button>