JS打印页面指定区域
错误的写法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//打印 function printPage(areaId) { if (parent.$("#PrinFrame").length == 0) { parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>'); } var prinFrame = parent.$("#PrinFrame")[0]; $(prinFrame).contents().find("body").html($("#" + areaId).html()); var win = prinFrame.contentWindow; win.document.execCommand('Print'); }
错误原因:只把打印区域的内容放到iframe中,样式信息丢了。
改进后的写法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//打印 function printPage(areaId) { if (parent.$("#PrinFrame").length == 0) { parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>'); } var prinFrame = parent.$("#PrinFrame")[0]; var win = prinFrame.contentWindow; $(prinFrame).attr("src", window.location.href); $(prinFrame).load(function () { $(prinFrame).contents().find("body").html($("#" + areaId).html()); win.document.execCommand('Print'); }); }
在iframe中重新加载当前页面,然后把body中的内容替换成待打印区域,这样iframe中保留了样式信息。
上面写法的缺点:多次点击打印按钮,iframe的load事件会被绑定多次;打印区域的大小超出A4纸范围;
再次改进后的写法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//打印 function printPage(areaId) { var prinFrame; var win; if (parent.$("#PrinFrame").length == 0) { parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>'); prinFrame = parent.$("#PrinFrame")[0]; win = prinFrame.contentWindow; $(prinFrame).load(function () { setTimeout(function () { var html = '<table style="width:970px;"><tr><td>'; html += $("#" + areaId).html(); html += '</td></tr></table>'; $(prinFrame).contents().find("body").html(html); win.document.execCommand('Print'); }, 100); }); } else { prinFrame = parent.$("#PrinFrame")[0]; } $(prinFrame).attr("src", window.location.href); }
再次改进后,确保iframe的load事件只被绑定一次;用宽度为970的table限制打印区域大小。
上面的写法还是有错误,重新打开tab页时,点击打印,不再进入iframe的load方法,再修改:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//打印 function printPage(areaId) { if (parent.$("#PrinFrame").length == 0) { parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>'); } parent.$("#PrinFrame").attr("src", window.location.href); parent.$("#PrinFrame").one("load", function () { setTimeout(function () { var html = '<table style="width:970px;"><tr><td>'; html += $("#" + areaId).html(); html += '</td></tr></table>'; parent.$("#PrinFrame").contents().find("body").html(html); parent.$("#PrinFrame")[0].contentWindow.document.execCommand('Print'); }, 100); }); }
弄了一天,分页打印的时候还是有问题,如下图:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库