簡體   English   中英

當打印頁面出現在javascript中時,如何刪除“日期”和“無標題”字母?

[英]How to remove 'Date' and 'Untitled' letters when print page appears in javascript?

當按下按鈕時,我出現了打印頁面。但是我在打印頁面中得到了不必要的東西,例如:

在此處輸入圖片說明

如何刪除這些字段?

我的運行代碼是(代碼段不起作用,您可以直接復制並粘貼並運行它):

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">
        function printDiv(divID) {
            //Get the HTML of div
            var divElements = document.getElementById(divID).innerHTML;
            //Get the HTML of whole page
            var oldPage = document.body.innerHTML;

            //Reset the page's HTML with div's HTML only
            document.body.innerHTML = 
              "<html><head><title></title></head><body>" + 
              divElements + "</body>";

            //Print Page
            window.print();

            //Restore orignal HTML
            document.body.innerHTML = oldPage;


        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div id="printablediv" style="width: 100%; background-color: Blue; height: 200px">
        Print me I am in 1st Div
    </div>
    <div id="donotprintdiv" style="width: 100%; background-color: Gray; height: 200px">
        I am not going to print
    </div>
    <input type="button" value="Print 1st Div" onclick="javascript:printDiv('printablediv')" />
    </form>
</body>
</html>

要刪除標題,您需要將空白設置為零。

<style type="text/css" media="print">
        @page 
        {
            size:  auto;
            margin: 0mm;
        }
    </style>

您只需要添加一些CSS。

@page { size: auto;  margin: 0mm; }

https://jsfiddle.net/mfLsa82q/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM