之前幫老師做一個小模塊,需要有一個是打印功能,我就在網上找了很多材料,其中一個方法是通過javascript打印網頁中的指定內容,這個方法相對來說比較簡單,但是,只要能夠完成需求,它就是一個好方法。
具體的代碼如下,我是寫在jsp頁面中的,當然,也可以應用在其他例如html或者asp等。
<head>
<script language="JavaScript">
<!--
function doPrint() {
bdhtml = window.document.body.innerHTML;
sprnstr = "<!--startprint-->";
eprnstr = "<!--endprint-->";
prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
window.document.body.innerHTML = prnhtml;
window.print();
location.reload();
}
//-->
</script>
</head>
<body>
<!--startprint-->
<table width="700" align="left" border="1">
<tr>
<td>待打印的內容</td>
</tr>
</table>
<!--endprint-->
<input type="button" οnclick="doPrint()">
</body>