javascript - Css 在 javascript 打印功能中不起作用

标签 javascript html css printing

我有一个javascript函数来打印一个由按钮调用的特定div。它像我想要的那样工作但没有样式。问题是当我尝试将div样式添加到该函数时,打印预览显示空白页Chrome(Firefox 运行良好)。我的功能代码是:

    function printDiv(event) {
var DocumentContainer = document.getElementById("page-wrap");

   var html = '<html><head>'+
              '<link href="./css/style.css" rel="stylesheet" type="text/css"/>'+
              '</head><body style="background:#ffffff;">'+
              DocumentContainer.innerHTML+'</body></html>';

    var WindowObject = window.open("", "PrintWindow",
    "width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
    WindowObject.document.writeln(html);
    WindowObject.document.close();   
    WindowObject.print();
    WindowObject.close(); 
}

谁能帮帮我?

最佳答案

使用css媒体查询

 @media print

并在打印中应用您想要的样式

关于javascript - Css 在 javascript 打印功能中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42561182/

相关文章:

jquery - CSS3改造后点击不来

javascript - jQuery,从变量调用数组对象的一部分

html - bootstrap4 中网格/列之间的空间

单击后 CSS 菜单不会保持事件状态

html - Bootstrap : nav items font color doesn't change according to theme

javascript - 子元素样式 CSS

html - 强制一个空的 div 占据整个页面的高度

php - 在 php SESSION 中使用空 - 0 例

javascript - 自匿名函数在加载时运行绑定(bind)事件

javascript - 当用户单击缩略图时如何显示完整图像 ASP.NET