js实现打印功能

window.print()执行打印功能

//打印后执行的操作window.onafterprint

//打印前执行的操作window.onbeforeprint

<body>
    <!--startprint-->
    <div class="x-body">
        <div class="noprint">
            <div onclick="window.history.go(-1);" style="" type="button" class="layui-btn layui-btn-normal">返回上一页</div>
            <div onclick="doPrint()" type="button" class="layui-btn">打印</div>
        </div>
        
        
        <div class="" style="margin-top:20px;">
            <div class="" style="text-align:center;">
                <span style="font-size:22px;">产品销售合同</span>
                <span style="font-size:16px;">(代发货订单)</span>
            </div>
            <div class="" style="">
            
            </div>
        </div>
    </div>
    <!--endprint-->
<script>

function doPrint() {
  $(".noprint").css("display",'none');
  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();

}
//打印后
window.onafterprint = function(){
    console.log("Printing completed...");
    $(".noprint").css("display",'block');
    window.location.reload();
}
//打印前执行某些操作
window.onbeforeprint = function(){
   console.log("Printing onbeforeprint...");
   //record();
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值