web打印 window.print()介紹
我只給出比較有效的,方便的打印方法,有些WEB打印是調(diào)用ActiveX控件的,這樣就需要用戶去修改自己IE瀏覽器的Internet選項里的安全里的ActiveX,將它們?nèi)繂⒂?,有些麻煩,翻了下網(wǎng)絡(luò),下面的方法是可以直接打印,而不會去修改IE的Internet選項。
window.print來打印頁面,頁面上別的元素也會被打印處理,頁頭頁尾的格式也不好控制。
• 常用方法:大部分情況會把查詢的結(jié)果綁定到DataGrid上來,然后打印DataGrid。這種情況的打印一般來說格式比較固定簡單,確定后基本不會再作更改。所以可以采用IE直接打印。
【實例代碼】
注:①這是客戶端通過window.print打印指定內(nèi)容。這里定義sprnstr和eprnstr來指定內(nèi)容
執(zhí)行代碼:
<input type="button" name="print" value="http://www.dbjr.com.cn/yc1990/archive/2012/03/03/預(yù)覽并打印" onclick="preview()">
②如果直接使用window.print將打印頁面上的所有內(nèi)容,但是我們可以使用
<style> @media Print { .Noprn { DISPLAY: none }}
是用來指定不打印的內(nèi)容。
<script language="Javascript">
function preview()
{
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();
}
</script>
<!--省略部分代碼-->
<form id="WebForm1" method="post" runat="server">
<center>本部分以上不被打印</center>
<!--startprint-->
<div align="center">
<asp:DataGrid id="dgShow" runat="server">
<!--省略部分代碼-->
</asp:DataGrid>
</div>
<!--endprint-->
<center>本部分以下不被打印</center>
<div align="center">
<input type="button" name="print" value="http://www.dbjr.com.cn/yc1990/archive/2012/03/03/預(yù)覽并打印" onclick="preview()">
</div>
<style> @media Print { .Noprn { DISPLAY: none }}
</style>
<p class="Noprn">不打印</p>
<table id="datagrid">
<tr>
<td>打印</td>
</tr>
</table>
<input class="Noprn" type="button" onclick="window.print()" value="http://www.dbjr.com.cn/yc1990/archive/2012/03/03/print">
</form>
最主要的一句就是:
<input class="Noprn" type="button" onclick="window.print()" value="http://www.dbjr.com.cn/yc1990/archive/2012/03/03/print">
這樣就可以打印了,通過設(shè)置CSS屬性,將很容易控制哪些需要打印,哪些不需要打印,而且這樣的打印是沒有附加的頁眉和頁碼的信息的。