【问题标题】:Open print window without content打开没有内容的打印窗口
【发布时间】:2020-01-08 17:10:15
【问题描述】:

我有一个按钮可以打开打印窗口并设置要打印的内容。单击按钮时会触发 JS 功能。问题是打印窗口在没有“内容”的情况下打开,所以它只打印一个空页面。如果我来到打印窗口并单击 Control + P 它工作正常。如何在打开打印窗口之前设置内容?

function printApp(){
   var content = document.documentElement.innerHTML;
   content = content.replace('<input type=\"button\" class=\"btn btn-success\" value=\" Print Application \" onclick=\"printApp();\">', '');                          
   var mywindow = window.open('', 'Print', 'height=600,width=800');

   mywindow.document.write('<html>');
   mywindow.document.write(content);
   mywindow.document.write('</html>');
   mywindow.document.close();
   mywindow.focus()
   mywindow.print();
   //mywindow.close();
   return true;                     
}  

【问题讨论】:

  • 不明白为什么......看起来一切都很好
  • 它适用于 Safari,但不适用于 Firefox

标签: javascript


【解决方案1】:

我知道您想动态设置打印布局,但您似乎也只想在打印前从内容中删除一个按钮。这可以通过 CSS 媒体查询来实现:

@media print {
    .btn.btn-success {
        display : none;
    } 
}

当打印窗口处于活动状态时,这将从 DOM 中删除任何带有 btn btn-success 类的按钮。

【讨论】:

  • 我认为这是关于从内容中删除按钮的显示,但我仍然遇到打印以空白页面打开的问题
  • 我知道,因为您是在没有内容的新标签页中打开它。如果您使用上面的媒体查询而不调用window.open(),它将起作用,因为它将在当前页面上打开,其中包含您的可打印内容。
  • 还是不行。顺便说一句,问题发生在 Firefox 上,但在 Safari 上工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 2010-10-02
  • 2012-08-24
相关资源
最近更新 更多