【问题标题】:Creating Multiple pages with different text to print with javascript创建具有不同文本的多个页面以使用 javascript 打印
【发布时间】:2020-02-28 17:06:37
【问题描述】:


所以我正在创建一个基本的 asp.bet 网站。最后,生成一张收据以像这样打印 -

所以我用简单的 javascript 代码打印这张收据。 javascript 代码如下-

   <script type="text/javascript">
            function printData() {
                if (confirm("Data Saved!\nDo you want to take a Print?")) {
                    var divToPrint = document.getElementById("<%=divAll.ClientID %>");
                    newWin = window.open("");
                    newWin.document.write(divToPrint.outerHTML);
                    newWin.document.close();
                    newWin.focus();
                    newWin.print();
                    newWin.close();
                }
            }
    </script>

(上面的收据编码在表格中,“divAll”是包含所有表格的主要部门。文本CONSIGNEE COPY是表格部门中的简单文本。)
打印按钮的代码如下-

<asp:Button ID="btnPrint" runat="server" Text="Print Consignment" OnClientClick="printData();"/>

我要做的是拿 4 份带有不同文本的收据副本代替 CONSIGNEE COPY-

1- 收货人副本
2- 驱动程序副本
3- 税务副本
4- 办公室副本

所以现在我的问题是,我可以用 javascript 做到这一点吗?
javascript 的 printData() 函数已经打开了一个新窗口。我可以在 newWin 中使用相同的收据但使用不同的文本(代替 CONSIGNEE COPY)附加一个新页面。所以最后 newWin 有 4 个页面,每个页面上都有不同的文本。那可能吗?我怎样才能做到这一点?

感谢您的帮助。 :)

#编辑:
好的,经过一番搜索,我设法在打印预览中添加了两次收据。

<script type="text/javascript">
            function printData() {
                if (confirm("Data Saved!\nDo you want to take a Print?")) {
                    var divToPrint = document.getElementById("<%=divAll.ClientID %>");                    
                    newWin = window.open("");
                    newWin.document.write(divToPrint.outerHTML);
                    var div = document.createElement("div");
                    div.innerHTML = divToPrint.innerHTML;
                    div.setAttribute("style", "font-family:Arial !important;");
                    div.setAttribute("align", "center");
                    newWin.document.body.appendChild(div);
                    newWin.document.close();
                    newWin.focus();
                    newWin.print();
                    newWin.close();
                }
            }
    </script>

通过上述方法我也可以添加4页,但是如何每次更改CONSIGNEE COPY值?请帮忙!

【问题讨论】:

    标签: javascript html asp.net printing


    【解决方案1】:

    试试这个

    只需更改一行,然后迭代其他剩余副本

        newWin.document.write(divToPrint.outerHTML.replace("CONSIGNEE COPY", "DRIVER COPY"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多