Chrome在打印预览中出现样式问题

时间:2018-10-27 21:23:20

标签: javascript html css html5 css3

以下代码在Firefox Mozilla上都可以正常运行,但在Chrome或IE中无法正常运行

printWindow.document.write('<link rel="stylesheet" href="/css/styles.css">');

控制台中的消息是:

  

[不推荐使用]用HTML定义的样式表样式化主文档   导入已弃用

如果我这样做的话,它将适用于所有浏览器:

printWindow.document.write('<style>body {background:red}</style>');

任何想法我将如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解您的问题,但是我确实将以下Pen放在了一起,以作为解决该问题的示例。如您所见,我能够在新窗口中使用外部CSS库。在Chrome 70和Edge中进行了测试,但是我没有可靠的方法在IE中进行测试。

您可以忽略javascript从原始窗口中提取内容的部分,我只是假设这是您要使用的功能。

var printWindow = window.open("", "PrintWindow", "width=400,height=400");
printWindow.document.write('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitterbootstrap/4.1.2/css/bootstrap.min.css">');