var headstr = “
var footstr = “”;
var newstr = divhead + divbody + divfoot;
var printInfos = headstr + newstr + footstr;
BasePrint(printInfos);
return false;
function BasePrint(printInfos) {
var iframe = document.getElementById(“print-iframe”);
if (!iframe) {
iframe = document.createElement(‘IFRAME’);
var doc = null;
iframe.setAttribute(“id”, “print-iframe”);
iframe.setAttribute(‘style’, ‘position:absolute;width:0px;height:0px;left:-500px;top:-500px;’);
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
debugger
//这里可以自定义样式
doc.write(printInfos);
doc.close();
iframe.contentWindow.focus();
}
// 延迟200毫秒加载打印
setTimeout(function () {
iframe.contentWindow.print();
document.body.removeChild(iframe);
}, 200);
}
本文介绍了一种使用JavaScript实现网页内容打印的方法。通过创建隐藏的iframe元素,并将需要打印的内容写入该iframe中,然后调用print方法实现打印功能。文章详细展示了如何构建打印所需的信息结构及如何触发打印。
781

被折叠的 条评论
为什么被折叠?



