当前位置: 首页 > 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.rkmt.cn/news/56375.html

相关文章:

  • Boost都有哪些功能
  • 【ESSC|连续三届检索】第四届教育科学与社会文化国际学术会议(ESSC 2025)
  • 2025年市场热门的河道护坡石笼网公司怎么选择,抗冲击抗腐蚀石笼网/柔韧抗压石笼网/双隔板石笼网河道护坡石笼网直销厂家有哪些
  • 2025年深圳废旧18650电池回收公司权威推荐榜单:动力18650电池回收/大量回收18650锂电池/18650电池组回收源头公司精选
  • gdb linux
  • 《从成本中心到价值创造:QMS系统的商业价值重构》‌
  • 浙江 GEO 企业 TOP4 榜单:解码 AI 时代的智能营销新势力
  • 视频汇聚平台EasyCVR进程启动后视频却无法播放的原因排查
  • 2025 最新雕塑厂家推荐榜:涵盖商业街 / 校园 / 公园 / 商场 / 广场雕塑等多场景优质雕塑企业权威推荐小区雕塑/广场雕塑/场景雕塑公司推荐
  • Day8:2025年9月29日,星期一,上班。
  • 电梯调度程序的三次作业分析
  • MySQL索引详解 - 指南
  • 2025年螺杆空压机制造企业权威推荐榜单:二手螺杆机/空压机配件/空压机维修供应商精选
  • 2025 年 北京VI 设计公司最新推荐榜:优质服务商全维度解析,助力品牌资产高效增长
  • Mounriver Studio设置为工程默认加载路径(Ⅰ代\Ⅱ代)
  • 基于回归分析法的光伏发电系统最大功率计算simulink建模与仿真
  • gcc下载 linux
  • 广州知名的产品认证办理流程,3A信用认证/ISO22000/产品测试报告/3C认证/CE认证/REA认证产品认证申请流程
  • 2025年国内PMS酒店管理系统公司综合实力排行榜TOP10
  • 2025 最新办公桌椅优质厂家推荐排行榜:专利加持 + 政企集采热门,广东办公座椅/广东办公桌/实木办公桌/现代办公桌/总裁办公桌公司推荐
  • 2025年PMS酒店管理系统公司全方位评测与推荐榜单
  • 实用指南:软件设计师知识点总结:操作系统
  • 2025年PMS酒店管理系统公司排行榜Top10:智能化解决方案权威推荐
  • 2025年11月国内PMS酒店管理系统公司综合评测与推荐榜单
  • 2025 年 11 月彩盒包装盒,彩盒印刷包装盒,茶叶礼盒包装盒厂家最新推荐,实力品牌深度解析采购无忧之选!
  • min-document原型污染漏洞分析与修复
  • gcc linux安装
  • gcc linux
  • L1 Loss、L2 Loss、Cross-Entropy Loss
  • ARM AXI-stream、ACE-Lite 与 CMN 的区别解析 - ENGINEER