前言
打印功能兼容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
代码如下(示例):
<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);
}