简体   繁体   English

在不打开新标签的情况下打印HTML

[英]Print HTML without opening in a new tab

Without rendering the HTML first, just print the HTML without opening a new tab, is that possible? 无需先渲染HTML,而无需打开新标签就可以打印HTML,这可能吗? Below is my existing code that works printing in a new tab: 以下是我现有的可在新标签页中打印的代码:

  var w = window.open();
  var html = $(data).html();

  $(w.document.body).html(html);
  w.print();
  • remove window.open(); 删除window.open();
  • example

Get Print Data 获取打印数据

var printData = document.getElementById('printID').innerHTML;

Get the HTML of whole page 获取整个页面的HTML

var originalData = document.body.innerHTML;

Add printData to html 将printData添加到html

document.body.innerHTML =
 "<html><head><title></title></head><body>" +
 printData + "</body>";

Print Page 打印页面

window.print();

Restore orignal HTML 恢复原始HTML

document.body.innerHTML = originalData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM