js打印表单时,为什么表单内容修改后有的不起效?-灵析社区

时光旅人

js打印表单时,为什么表单内容修改后有的不起效? **如下:** **页面填写:** ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/c330d89e0d0f7710c2b8a3da88f412c8.png) **实际打印** :textarea内容不显示, 复选框也不显示高亮 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/4347fac33875a8bc9bf170f80476d330.png) 源码demo: Document 正位 外显斜 内显斜 外隐斜 内隐斜 点我打印 document.getElementById('dw').addEventListener('click', function() { // window.print(); let docHtml1 = '' docHtml1 += $("#divKanZhengPanel-binli").prop("outerHTML"); $('#print-iframe').remove(); // 每次打印前移除先前生成的元素 // 开始打印 let iframe1 = document.createElement('IFRAME'); let doc1 = null; iframe1.setAttribute("id", "print-iframe"); iframe1.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-0px;top:-0px;visibility: auto;'); document.body.appendChild(iframe1); doc1 = iframe1.contentWindow.document; doc1.write(docHtml1); doc1.close(); iframe1.contentWindow.focus(); iframe1.contentWindow.print(); })

阅读量:15

点赞量:0

问AI
你这里之前时对外面的盒子调用了 outerHTML ,是拿不到实际的值的(你直接把结果 console.log 出来就能看到了),你点击那个按钮打印看到的预览效果也应该是没有的,至少我这里是这样。 你应该使用 cloneNode ,去克隆你原来的节点。 Document 正位 外显斜 内显斜 外隐斜 内隐斜 点我打印 document.getElementById('dw').addEventListener('click', function () { $('#print-iframe').remove(); // 每次打印前移除先前生成的元素 // 开始打印 let iframe1 = document.createElement('IFRAME'); let doc1 = null; iframe1.setAttribute('id', 'print-iframe'); iframe1.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-0px;top:-0px;visibility: auto;'); document.body.appendChild(iframe1); setTimeout(function () { doc1 = iframe1.contentWindow.document; doc1.body.appendChild(document.querySelector('#divKanZhengPanel-binli').cloneNode(true)); doc1.close(); iframe1.contentWindow.focus(); iframe1.contentWindow.print(); }); }); 打印效果: "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241206/7d6a916b9afd172b0c1cbe71f78932e9.png)