javascript - 使用 Javascript 打印外部 HTML 文件

标签 javascript html printing

我在 index.html 上有一个“打印”按钮。我需要什么代码来打印 print.html 文件?我的意思是,当我按下 index.html 中的按钮时,打印页面 print.html

最佳答案

function closePrint () {
  document.body.removeChild(this.__container__);
}

function setPrint () {
  this.contentWindow.__container__ = this;
  this.contentWindow.onbeforeunload = closePrint;
  this.contentWindow.onafterprint = closePrint;
  this.contentWindow.focus(); // Required for IE
  this.contentWindow.print();
}

function printPage (sURL) {
  var oHiddFrame = document.createElement("iframe");
  oHiddFrame.onload = setPrint;
  oHiddFrame.style.visibility = "hidden";
  oHiddFrame.style.position = "fixed";
  oHiddFrame.style.right = "0";
  oHiddFrame.style.bottom = "0";
  oHiddFrame.src = sURL;
  document.body.appendChild(oHiddFrame);
}

然后使用

onclick="printPage('print_url');"

关于javascript - 使用 Javascript 打印外部 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168725/

相关文章:

javascript - ReactJS - 在所有数据输入状态后将我的加载更改为 false

javascript - 如何根据 bool Prop 和名称 Prop 对数组进行排序?

javascript - 除了使用自定义 CSS 和 html 之外,还有其他方法可以显示从 API 获取的推文吗?

javascript - 如何将 div 添加到 React Native?

c++ - 为什么 gdb 中的打印命令会为 C++ std::strings 返回\035?

php - 根据 ID 表将单独的数组结果发送到打印机

javascript - 如何打印nodejs程序的调用堆栈大小

javascript - 从其他页面获取 HTML 并将脚本附加到 "type=' 模块后'“它只工作一次然后停止

javascript - 如何通过非立即的类获取父元素的ID值? jQuery

Python 3.6.1 : How to dynamically print a docstring?