Javascript 从 html 页面打印表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10088890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:49:00  来源:igfitidea点击:

Print a table from an html page

javascripthtmlprinting

提问by user1263746

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. Any ideas?

我有一个 html 页面,需要从中将表格发送到打印机。我现在正在使用 window.print .. 但这会打印整个页面......而我只需要打印表格。有任何想法吗?

回答by yAnTar

  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.

  1. 您可以使用媒体类型打印这里是提示,如何使用样式表打印 html 页面)。

  2. 您可以通过弹出窗口实现这一点 - 在此窗口中仅显示表格并将其发送到打印机。

Simple example

简单的例子

<script>
    function printDiv() {
        var divToPrint = document.getElementById('areaToPrint');
        newWin = window.open("");
        newWin.document.write(divToPrint.outerHTML);
        newWin.print();
        newWin.close();
   }
</script>

回答by Sree

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

您可以为display:none页面的所有不需要的部分提供样式。这样你只能打印表格。

for ex:

例如:

<style>
    @media only print {
        footer, header, .sidebar {
            display:none;
        }
    }
</style>

回答by aceror

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

为了使它工作,我还需要在我的文档的 head 部分之间放置这条线。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>