繁体   English   中英

使用javascript打印DIV的内容

[英]Print the content of the DIV using javascript

我目前正在执行一项任务,以在div中打印内容。 目前,我正在使用这种方法 如果div小于屏幕,则可以正常工作。 我的问题是,我需要打印(报告)的div是普通屏幕高度的两倍或三倍。

当我按Control + P时,我只能看到的只是在屏幕上捕获的内容。

使用打印查询怎么办?

@media print {
  .selector{
    /*some css*/
  }
}

您可以使用它并在打印过程中重新排列div的内容

这是示例代码示例,希望对您有所帮助。 如果您需要其他东西,请随时发表评论。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("#btnPrint").live("click", function () {
            var divContents = $("#dvContainer").html();
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>DIV Contents</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        });
    </script>
</head>
<body>
    <form id="form1">
    <div id="dvContainer">
        This content needs to be printed.
    </div>
    <input type="button" value="Print Div Contents" id="btnPrint" />
    </form>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM