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
Print a table from an html page
提问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
You can use media types print(here is tips, how print html page using stylesheets).
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>
回答by Sree
You can give style display:none
to 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>