当打印页面出现在javascript中时,如何删除“日期”和“无标题”字母?

时间:2018-11-30 04:25:23

标签: javascript html

按下按钮后会出现打印页面。但是我在打印页面上看到不必要的东西,例如:

enter image description here

如何删除这些字段?

我的运行代码是(代码段不起作用,您可以直接复制并粘贴并运行它):

<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>

2 个答案:

答案 0 :(得分:1)

要删除标题,您需要将边距设置为零。

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

答案 1 :(得分:1)

您只需要添加一些CSS。

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

https://jsfiddle.net/mfLsa82q/