Print a table from an html page

前端 未结 3 909
南方客
南方客 2021-02-05 07:44

I have a html page, from which a table needs to be sent to a printer. I am using window.print right now.. but that prints the whole page... while I need to print just the table.

相关标签:
3条回答
  • 2021-02-05 07:49
    1. You can use media types print (here is tips, how print html page using stylesheets).

    2. You can realize that through popup window - in this window show only table and send it to printer.

    Simple example

    <script>
        function printDiv() {
            var divToPrint = document.getElementById('areaToPrint');
            newWin = window.open("");
            newWin.document.write(divToPrint.outerHTML);
            newWin.print();
            newWin.close();
       }
    </script>
    
    0 讨论(0)
  • 2021-02-05 07:51

    You can give style display:none to all unwanted portions of the page. In that way you can print only table.

    for ex:

    <style>
        @media only print {
            footer, header, .sidebar {
                display:none;
            }
        }
    </style>
    
    0 讨论(0)
  • 2021-02-05 07:58

    To make it work I need also to put this line between the head section in my document.

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    0 讨论(0)
提交回复
热议问题