web打印 window.print()介绍

有些WEB打印是调用ActiveX控件的,这样就需要用户去修改自己IE浏览器的Internet选项里的安全里的ActiveX,将它们全部启用,有些麻烦,翻了下网络,下面的方法是可以直接打印,而不会去修改IE的Internet选项。

window.print来打印页面,页面上别的元素也会被打印处理,页头页尾的格式也不好控制。
• 常用方法:大部分情况会把查询的结果绑定到DataGrid上来,然后打印DataGrid。这种情况的打印一般来说格式比较固定简单,确定后基本不会再作更改。所以可以采用IE直接打印。

【实例代码】
注:①这是客户端通过window.print打印指定内容。这里定义sprnstr和eprnstr来指定内容
执行代码:
<input type="button" name="print" value="http://www.jbxue.com/yc1990/archive/2012/03/03/预览并打印" οnclick="preview()">
    
②如果直接使用window.print将打印页面上的所有内容,但是我们可以使用 
<style> @media Print { .Noprn { DISPLAY: none }}
是用来指定不打印的内容。
<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.jbxue.com/yc1990/archive/2012/03/03/预览并打印" οnclick="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" οnclick="window.print()" value="http://www.jbxue.com/yc1990/archive/2012/03/03/print">
</form>

核心:
<input class="Noprn" type="button" οnclick="window.print()" value="http://www.jbxue.com/yc1990/archive/2012/03/03/print">
这样就可以打印了,通过设置CSS属性,将很容易控制哪些需要打印,哪些不需要打印,而且这样的打印是没有附加的页眉和页码的信息的。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值