首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js的print()打印步骤使用小结

2012-06-28 
js的print()打印方法使用小结先调用xx.window.focus()方法得到想要打印的JSP部分数据(xx代表要得到的数据,

js的print()打印方法使用小结

先调用xx.window.focus()方法得到想要打印的JSP部分数据(xx代表要得到的数据,如某一表格的id),再调用window.print()方法,就可以实现打印功能了,示例代码如下:

<%@ page contentType="text/html;charset=GBK"%>
<HTML>
<HEAD>
<script type="text/javascript">
<!--
function printinfobtn_onclick(){
?finfo.window.focus();???
???window.print();
???}
-->
</SCRIPT>
</HEAD>
<BODYoncontextmenu="self.event.returnValue=false">
<FORM action="" method=POST id=theForm name=theFormtarget=main>
<table border=0 style="98%"align=center><TR><TDalign=center><B><fontsize="4">出差申请单</font></B></TD></TR></table>
<TABLE? align="center" border=0cellPadding=1 cellSpacing=1 width="80%"bgcolor="#000000">
?? <iframeid=finfo name=finfo frameBorder=0 marginHeight=0marginWidth=0?>??</iframe>
? </table>
<br/>
<input? id=printbtn name=printbtntype="button" class=buttononclick="javascript:printinfobtn_onclick()"value="打印"/>
</FORM>
</BODY>
</HTML>
这里的printinfobtn_onclick()函数里面的finfo.window.focus()就是得到下边iframe标签的焦点

?当然,如果直接调用window.focus()方法,则表示得到整个JSP页面的数据。当我们不需要页面上的某个数据显示在打印文档里时,可以编写打印前和打印后的事件onbeforeprint、onafterprint或者在调用window.print()方法前设置目标标签的display属性值,来实现不打印非目标数据的功能。

function printinfobtn_onclick(){
?document.getElementByIdx_x_x("printbtn").style.display= 'none';
?finfo.window.focus();???
???window.print();
???document.getElementByIdx_x_x("printbtn").style.display='';
}

当这些都编写完成后,如果你的IE没有设置清空页眉页脚的话,还有可能会出来一些诸如:

http://localhost/×××/Action.do?orderId=......

这样的字样在你的文档里,这时就需要我们手动清空浏览器的页眉页脚,具体步骤如下:

打开命令行打开注册表编辑器,进入到以下目录:

js的print()打印步骤使用小结

我的电脑\HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

点开header、footer这些项自定义设置它的人值(建议header、footer设为空,其它的值可自定义),再确定、退出后,就可以看到没有页眉页脚的打印结果了。。。

?

?

打开注册表的命令是:regedit

热点排行