0

按下按钮时出现打印页面。但是我在打印页面中得到了不必要的东西,例如:

在此处输入图像描述

我怎样才能删除这些字段?

我的运行代码是(片段不工作你可以直接复制粘贴并运行它):

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

2 回答 2

1

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

<style type="text/css" media="print">
        @page 
        {
            size:  auto;
            margin: 0mm;
        }
    </style>
于 2018-11-30T04:31:46.797 回答
1

你只需要添加一些CSS。

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

https://jsfiddle.net/mfLsa82q/

于 2018-11-30T04:32:02.800 回答