简体   繁体   English

使用javascript打印DIV的内容

[英]Print the content of the DIV using javascript

I'm currently working on a task to print a content in a div. 我目前正在执行一项任务,以在div中打印内容。 Currently I'm using this approach . 目前,我正在使用这种方法 This works okay if the div is smaller than the screen. 如果div小于屏幕,则可以正常工作。 My problem is, my div that I need to print (report) is twice or thrice the height of the normal screen. 我的问题是,我需要打印(报告)的div是普通屏幕高度的两倍或三倍。

When I hit control + P. I can only see is what only captured in the screen. 当我按Control + P时,我只能看到的只是在屏幕上捕获的内容。

what about using a print query? 使用打印查询怎么办?

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

You can use this and re-arrange the content of your div in the print process 您可以使用它并在打印过程中重新排列div的内容

Here is sample code sample and hope it will be help you. 这是示例代码示例,希望对您有所帮助。 Feel free to comment if you need additional thing. 如果您需要其他东西,请随时发表评论。

<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