当前位置: 首页 > news >正文

宜搭在线js上点击按钮实现打印div效果

export function onClick() {console.log('onClick');const outerContainer = document.querySelector(".labelcode-tag-container");if (!outerContainer) {this.utils.toast({title: '请先展开二维码标签',type: 'error',});return;}const shadowRoot = outerContainer.shadowRootif (!shadowRoot) {this.utils.toast({title: '无妨访问shadow DOM',type: 'error',});return;}const printContainer = shadowRoot.querySelector('#sheet-container');// const printContainer = shadowRoot.querySelector('.tag-container');if (!outerContainer) {this.utils.toast({title: '未找到待打印区域',type: 'error',});return;}// 打印指定区域printElement(printContainer);
}
//打印指定DOM元素
function printElement(element) {//  创建打印 iframe(避免影响原页面样式)const iframe = document.createElement('iframe');iframe.style.position = 'absolute';iframe.style.top = '-9999px';iframe.style.left = '-9999px';iframe.style.width = '100%';iframe.style.height = '100%';iframe.style.zIndex = '9999';document.body.appendChild(iframe);// 复制待打印元素到iframeconst iframeDoc = iframe.contentDocument || iframe.contentWindow.document;// 复制原元素的所有内容(包括子节点)
//  iframeDoc.body.appendChild(element.cloneNode(true));iframeDoc.open();// 直接在 iframe 的 <head> 中写样式iframeDoc.write(`<html><head><meta charset="UTF-8"><title>打印预览</title><style>.tag {width: 133px;height: 200px;display: inline-block;background: #0089ff !important;border-radius: 8px !important;box-shadow: 0 0 10px 0 rgba(0, 0, 0, .1);}.tag-container{position: relative;height: 100%;width: 100%;background: #0089ff !important;}.tag-header{text-align: center;height: 30px;color: white;}.tag-header-content {padding: 8px 4px 0;}.title {font-size: 10px;line-height: 15px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}.tag-body {text-align: center;height: 157px;width: auto;margin: 0 4px;border-radius: 4px;background: white;position: relative;overflow: hidden;}.tag-body-left {width: 100%;position: absolute;top: 78px;left: 0;bottom: 5px;height: 74px;overflow: hidden;}.content, .var3, .var4, .var5, .var6 {font-size: 8px;line-height: 10px;min-height: 10px;height: auto;max-height: 20px;display: block;white-space: normal;overflow: hidden;text-overflow: ellipsis;margin-bottom: 5px;}.bold {font-weight: bold;}.qrcode {width: 67px;height: 67px;position: absolute;left: 33px;top: 37px;}.watermark {font-size: 5px;position: absolute;bottom: 4px;right: 4px;color: #C3C7CA;line-height: 6px;transform: scale(0.42);transform-origin: right bottom;}/* 强制打印背景色(核心代码) */@media print {body, .tag {/* 兼容 Chrome/Safari/Edge */-webkit-print-color-adjust: exact !important;/* 兼容 Firefox/IE */print-color-adjust: exact !important;/* 部分浏览器需要开启背景打印权限,这里强制声明 */background-print: always !important;}}</style></head><body><!-- 待打印内容 -->${element.innerHTML}</body></html>`);iframeDoc.close();//  打印完成后移除iframesetTimeout(() => {iframe.contentWindow.focus();iframe.contentWindow.print();setTimeout(() => {document.body.removeChild(iframe);}, 300);}, 500);
}
http://www.jsqmd.com/news/46686/

相关文章:

  • Boost都有哪些功能
  • 网页前端 加水印
  • CAN网关的作用到底是什么?(转载)
  • macos虚拟机-演示篇三配置clover/opencore引导
  • 2025年智适应Ai自习室市场前景与加盟投资指南
  • 题解:NFLSOI#31351. 小吃
  • xilinx在线升级+flash操作+N25Q128
  • Day23、24:2025年10月13日、14日,星期一、二,休息。
  • 【ESSC|连续三届检索】第四届教育科学与社会文化国际学术会议(ESSC 2025)
  • Day25:2025年10月15日,星期三,上班。
  • 【完结20章】AI Agent+MCP从0到1打造个人专属编程智能体
  • 2025年市场热门的河道护坡石笼网公司怎么选择,抗冲击抗腐蚀石笼网/柔韧抗压石笼网/双隔板石笼网河道护坡石笼网直销厂家有哪些
  • 2025年深圳废旧18650电池回收公司权威推荐榜单:动力18650电池回收/大量回收18650锂电池/18650电池组回收源头公司精选
  • gearman如何实现负载均衡
  • gdb安装 linux
  • gdb linux
  • 对数几率回归算法伪代码
  • 2025 年质量好的四川球墨铸铁管 top 品牌厂家排行榜
  • Day16:2025年10月6日,星期一,值班,万事顺遂。
  • 《从成本中心到价值创造:QMS系统的商业价值重构》‌
  • 2025 年评价高的四川自助洗车机厂家实力及用户口碑排行榜
  • 《避开这7个坑,你的QMS项目就成功了一半》‌
  • 【Springer|EI、SCOPUS双检索】第三届人工智能安全与隐私国际学术会议(AISP 2025)
  • 浙江 GEO 企业 TOP4 榜单:解码 AI 时代的智能营销新势力
  • C++ 中打开记录的多种方式及相关流类
  • 视频汇聚平台EasyCVR进程启动后视频却无法播放的原因排查
  • Day8:2025年9月29日,星期一,上班。
  • 电梯调度程序的三次作业分析
  • 从花果山到文明镜鉴:一位元诗人的AI元人文构想之路
  • 2025年螺杆空压机制造企业权威推荐榜单:二手螺杆机/空压机配件/空压机维修供应商精选