php 如何在网页上添加打印按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11634153/
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
How to add a print button to a web page
提问by Anil
On one of my pages in my website(coded in php), i'm trying to add 2 (even more) print buttons each one printing a portion of the webpage. For example, on a page there is a table with some values and 2 graphs underneath it. I would like to add 2 print buttons, one button printing the table and the other one printing both the graphs. I've found this Examplebut could not understand clearly. Any help or examples would help me.
在我网站的其中一个页面上(用 php 编码),我试图添加 2 个(甚至更多)打印按钮,每个按钮打印网页的一部分。例如,在页面上有一个表格,其中包含一些值和下面的 2 个图表。我想添加 2 个打印按钮,一个按钮打印表格,另一个按钮打印两个图形。我找到了这个例子,但无法清楚地理解。任何帮助或例子都会帮助我。
Thanks in advance.
提前致谢。
回答by Ed Manet
This is html/javascript code that will launch the browser's Print dialog when clicked.
这是 html/javascript 代码,单击时将启动浏览器的打印对话框。
<button onClick="window.print()">Print this page</button>
回答by wbcrimson
If your table is inside a divwith id='printTable' use:
如果您的表位于id='printTable'的div 内,请使用:
<a href="#null" onclick="printContent('printTable')">Click to print table</a>
EDIT: Here is the function "printContent()"
编辑:这是函数“printContent()”
<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Print Page</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}
//-->
</script>
回答by Fabrizio Calderan
A simple idea is to create some previous css printing rules ad hoc, e.g.
一个简单的想法是创建一些以前的 css 打印规则,例如
.printtable * { display : none; }
.printtable table { display : block; }
and
和
.printtableandgraph * { display : none; }
.printtableandgraph img { display : block; }
then the two buttons should add a .printtableand a .printtableandgraphclass for the body element (or other element containing your table and graphs) just before window.print()statement
然后这两个按钮应该在语句之前为 body 元素(或其他包含表格和图形的元素)添加一个.printtable和一个.printtableandgraph类window.print()
(if user click twice or more on the same print button check previously if the element has already set that class name)
(如果用户在同一个打印按钮上单击两次或多次,请检查元素是否已经设置了该类名)
回答by Piyush Tiwari
Print Button
打印按钮
<button onClick="window.print()">Print this page</button>`
Use This code for print any webpage there is only one draw back its print all the button and menu bar.
使用此代码打印任何网页只有一个撤消其打印所有按钮和菜单栏。
回答by Trusty Scout
To print a portion of the page, you can use a print.css and the print button for a quick and simple approach.
要打印页面的一部分,您可以使用 print.css 和打印按钮来实现快速而简单的方法。
- Set up print.css file and include:
- 设置 print.css 文件并包括:
body {visibility:hidden;}
.print {visibility:visible;}
Add the .print classto the appropriate portions of your page.
Add button:
将 .print 类添加到页面的适当部分。
添加按钮:
<button onClick="window.print()">Print this page ?</button>
Print results are neat and clean.
打印结果整洁干净。

