打印和预览功能兼容IE、谷歌、火狐,解决IE打印只显示第一页


前言

打印功能兼容IE、谷歌、火狐。


一、步骤

1.打印页面

在body层添加position:relative IE打印的时候就不会只显示第一页

代码如下(示例):

<!-- 添加position:relative IE打印的时候就不会只显示第一页-->
<body style="overflow: visible !important; position:relative">
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" id="wb" name="wb" width="0"></OBJECT>
<div id='divContent'>打印内容......</div>
<!-- 添加noprint 打印的时候就不显示-->
<div class="noprint">
<button onclick='IEVersion'>打印</botton>
</div>
</body>

2.引入JS

代码如下(示例):

jQuery.print.js Git下载地址

<script src="~/jquery/jquery-2.1.4.min.js"></script>
<!-- 这个适用于谷歌和火狐 -->
<script src="~/jQuery.print-master/jQuery.print.js"></script>

3.打印JS方法

代码如下(示例):

function IEVersion() {
      var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
      var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
      var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
      var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
      if(isIE) {
          var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
          reIE.test(userAgent);
          var fIEVersion = parseFloat(RegExp["$1"]);
          if(fIEVersion == 7) {
              printpreview();//调用的是IE原生自带的打印功能
          } else if(fIEVersion == 8) {
              printpreview();
          } else if(fIEVersion == 9) {
              printpreview();
          } else if(fIEVersion == 10) {
              printpreview();
          } 
      } else if(isEdge) {
          $("#divContent").print();//edge,调用的是jQuery.print.js的方法
      } else if(isIE11) {
          printpreview(); //IE11  
      }else{
          $("#divContent").print();//不是ie浏览器
      }
  }
  // execwb() 参数以及功能:
  //wb.execwb(1,1)//打开
   //wb.ExecWB(2,1);//关闭现在所有的IE窗口,并打开一个新窗口
   //wb.ExecWB(4,1)//;保存网页
   //wb.ExecWB(6,1)//打印
   //wb.ExecWB(7,1)//打印预览
   //wb.ExecWB(8,1)//打印页面设置
   //wb.ExecWB(10,1)//查看页面属性
   //wb.ExecWB(15,1)//好像是撤销,有待确认
   //wb.ExecWB(17,1)//全选
   //wb.ExecWB(22,1)//刷新
   //wb.ExecWB(45,1)//关闭窗体无提示
//打印预览
function printpreview(){
    wb.ExecWB(7,1);
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值