在我的网站中的一个页面(用php编码)上,我试图添加两个(甚至更多)打印按钮,每个按钮打印网页的一部分。例如,在一个页面上,有一个表,它下面有一些值和两个图形。我想添加两个打印按钮,一个按钮打印表,另一个按钮同时打印两个图。我找到了这个例子,但不能理解清楚。任何帮助或例子都会对我有所帮助。
提前谢谢。
这是html/javascript代码,单击后将启动浏览器的"打印"对话框。
<button onClick="window.print()">Print this page</button>
如果您的表位于div中,且id="打印表",请使用:
<a href="#null" onclick="printContent('printTable')">Click to print table</a>
编辑:这是函数"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>
一个简单的想法是临时创建一些以前的css打印规则,例如
.printtable * { display : none; }
.printtable table { display : block; }
和
.printtableandgraph * { display : none; }
.printtableandgraph img { display : block; }
然后这两个按钮应该在window.print()
语句之前为body元素(或包含表和图的其他元素)添加.printtable
和.printtableandgraph
类
(如果用户在同一打印按钮上单击两次或两次以上,请检查元素是否已经设置了该类名)
打印按钮
<button onClick="window.print()">Print this page</button>`
使用此代码打印任何网页只有一个撤回它的打印所有按钮和菜单栏。
要打印页面的一部分,可以使用print.css和打印按钮来实现快速简单的方法。
- 设置print.css文件并包括:
body {visibility:hidden;}
.print {visibility:visible;}
-
将.print类添加到页面的相应部分。
-
添加按钮:
<button onClick="window.print()">Print this page »</button>
打印结果整洁。