我在 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/