JS 使用 window对象的print方法实现分页打印

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41761540/article/details/83545976
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
//打印代码
   function Print()   
    {    
        //该方法是跳转新页面打印,因此需要在js中拼接一个新页面
		var printStr = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body >";
		var content = "";
 
		var str = document.getElementById('page1').innerHTML;     //获取需要打印的页面元素 ,page1元素设置样式page-break-after:always,意思是从下一行开始分割。
		content = content + str;
		str = document.getElementById('page2').innerHTML;     //获取需要打印的页面元素
		content = content + str;
		
        printStr = printStr+content+"</body></html>";                                              
        var pwin=window.open("Print.htm","print"); //如果是本地测试,需要先新建Print.htm,如果是在域中使用,则不需要
        pwin.document.write(printStr);
        pwin.document.close();                   //这句很重要,没有就无法实现  
        pwin.print();    
    }
</script>
</head>
 
<body >
<div><input type="button" value="打印" onclick="Print()" /></div>
<div id="page1">
		<table width="100%"  border="0" cellpadding="0" cellspacing="0"  style="page-break-after:always" >
        <tr><td>第一页打印内容</td></tr>
        </table>
</div>
<div id="page2">
		<table width="100%"  border="0" cellpadding="0" cellspacing="0" id="content" >
        <tr><td>第二页打印内容</td></tr>
        </table>
</div>
</body>
</html>

window.print() 设置打印样式和内容

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<style>
.printonly{
	display:none
}
@media print{
	input,.noprint{
		display:none
	}
	.printonly{
		display:block;
		width:50%
	}
}
/**
.printonly{
	display:none
}
不能写到这里,会导致打印时printonly的内容也不显示
*/
</style>
 
<body>
<div class="noprint">
	这段打印时不显示
</div>
<div class="printonly">
    <h1>这段打印时会显示</h1>
    <hr>
    This is print-only
</div>
<input onClick="print()" value="点击打印" type="button">
</body>
</html>

window.print()打印网页中指定内容的实现方法

<div class="widget am-cf"  ng-controller='attendancesCtrl'>
    <div class="widget-head am-cf noprint">
        <div class="widget-title  am-cf">考勤记录</div>
        <input type="button" value="打印"
               onclick="printpage()"/>
    </div>
    <div class="am-u-sm-12 printonly">
        <table class="am-table am-table-bordered am-table-compact">
            <thead>
                <tr>
                   <th>序号</th>
                   <th>用户账号</th>
                   <th>姓名</th>
                   <th>课程</th>
                   <th>出勤类别</th>
                   <th>打卡时间</th>
                </tr>
            </thead>
            <tbody>
             <!--startprint-->
                <tr  ng-repeat="attendances in attendancess">
                    <td>{{$index+1}}</td>
                    <td>{{attendances.suhItemcode}}</td>
                    <td>{{attendances.suhName}}</td>
                    <td>{{attendances.suhCourses}}</td>
                    <td ng-show="attendances.suhType==0" style="color: black">学生</td>
                    <td ng-show="attendances.suhType==1" style="color: blue">老师</td>
                    <td>{{attendances.suhCardtime}}</td>
                 </tr>
             <!--endprint-->
            </tbody>
         </table>
     </div>
</div>
<script type="text/javascript">

    function printpage()
    {
        bdhtml=window.document.body.innerHTML; 
        sprnstr="<!--startprint-->"; //开始打印标识字符串有17个字符
        eprnstr="<!--endprint-->"; //结束打印标识字符串
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); //从开始打印标识之后的内容
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); //截取开始标识和结束标识之间的内容
        window.document.body.innerHTML=prnhtml; //把需要打印的指定内容赋给body.innerHTML
        window.print(); //调用浏览器的打印功能打印指定区域
        window.document.body.innerHTML=bdhtml;//重新给页面内容赋值;
    }
</script>

猜你喜欢

转载自blog.csdn.net/weixin_41761540/article/details/83545976