JavaScript document.write CSS 不适用



HTML:

<div id="dvContainer">
        This content needs to be printed.
</div>
<button>Print</button>

JavaScript:

 $("button").on("click", function(){
    var divContents = $("#dvContainer").html();
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>DIV Contents</title>');
            printWindow.document.write("<link rel="stylesheet" type="text/css" href="/css/product.css" />");
            //printWindow.document.write("<style>#dvContainer{color: red;}</style>");
            printWindow.document.write('</head><body>');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
});

当窗口出现时,我需要我的div 更改为红色,但什么也没发生。

我尝试使用样式代码,但我的元素仍然是黑色的。我也尝试了媒体打印,但什么都没有。

有什么建议吗?我做错了什么?
我正在最新版本的Chrome和jQuery 1.9.1中对此进行测试。

在我们的谈话之后,您要做的是正确设置打印样式。将其添加到产品中.css

@media print {
   body {
      background-color: red;
   }
 }

最后我找到了解决方案!!引导代码的打印媒体类型阻止了我,也是阻止我页面中所有其他 CSS 的错误。所以我编译了一个没有打印介质类型的新引导程序,现在正在工作!!

最新更新