<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
        
            function do_print(id_str){
                //id-str 打印区域的id
                var el = document.getElementById(id_str);
                var iframe = document.createElement('IFRAME');
                var doc = null;
                iframe.setAttribute('style','position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
                document.body.appendChild(iframe);
                doc = iframe.contentWindow.document;
                // 引入打印的专有CSS样式,根据实际修改       
                //doc.write("<LINK rel=\"stylesheet\" type=\"text/css\" href=\"css/print.css\">");
                doc.write('<div>' + el.innerHTML + '</div>');
                doc.close();       
                iframe.contentWindow.focus();
                iframe.contentWindow.print();
                if (navigator.userAgent.indexOf("MSIE") > 0){
                    document.body.removeChild(iframe);      
                }
            }
        </script>
    </head>
    <body>
        <!--打印区域: -->
        <div id="print_box">
            <div style="width: 500px; height: 500px; border: 5px solid #6b3906;">
                <h1>hello kitty!</h1>
            </div>
        </div>
        <!--// 调用打印 -->
        <button onclick="javascript:do_print('print_box');">打印</button>
        
    </body>
</html>

实战项目中的使用

$scope.do_print = function (id_str) {
        //id-str 打印区域的id
        var el = document.getElementById(id_str);
        var iframe = document.createElement('IFRAME');
        var doc = null;
        iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
        document.body.appendChild(iframe);
        doc = iframe.contentWindow.document;
        // 引入打印的专有CSS样式,根据实际修改       
        //doc.write("<LINK rel=\"stylesheet\" type=\"text/css\" href=\"css/print.css\">");
        if ($scope.orderinfo.inv==null) {
            var txt = "table{border-collapse:collapse;border-spacing:0;border-left:1px solid #000000;border-top:1px solid #000000;} td{border: 1px solid #000000;}th{border: 1px solid #000000;}#changefapiao{visibility: hidden;} .change{border-right: 0px solid #000000;}.change1{border-left: 0px solid #000000;} .title{text-align:center;} #fapiao{display:none;}";
        }
        else {
            var txt = "table{border-collapse:collapse;border-spacing:0;border-left:1px solid #000000;border-top:1px solid #000000;} td{border: 1px solid #000000;}th{border: 1px solid #000000;}#changefapiao{visibility: hidden;} .change{border-right: 0px solid #000000;}.change1{border-left: 0px solid #000000;} .title{text-align:center;}";
        }
        doc.write('<style>' + txt + '</style>');
        doc.write('<body>' + el.innerHTML + '</body>');
        doc.close();
        iframe.contentWindow.focus();
        iframe.contentWindow.print();
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            document.body.removeChild(iframe);
        }
    }

最新文章

  1. JS入门学习,写一个时钟~
  2. yyyy-MM-dd与YYYY-MM-dd
  3. img图片下面出现莫名的下边距解决办法
  4. SQL内联多个表
  5. 调用webservice 417
  6. undo损坏故障恢复(二)ORA-01092,ORA-00604,ORA-01110
  7. 【转】V4L2+swscale+X264+live555实现流媒体服务端
  8. iscroll5 版本下的 上拉,下拉 加载数据
  9. Memcached&#183;Redis缓存的基本操作
  10. 数据库索引------Hash索引的使用限制
  11. Linux 安装多个版本JDK并设置默认版本
  12. Unity---在Hierarchy视图中将选中的对象的层级目录复制到剪切板
  13. jenkins 构建到最后报权限的问题
  14. Java二叉树的实现与特点
  15. jvm--深入理解java虚拟机 精华总结(面试)(转)
  16. mac burp suite https证书安装
  17. Bash数组
  18. ERROR 1045 (28000): Access denied for user &#39;hive&#39;@&#39;localhost&#39; (using password: YES)
  19. Tensorflow BatchNormalization详解:1_原理及细节
  20. GC之八--GC 触发Full GC执行的情况及应对策略

热门文章

  1. 工作中的Buff加成-结构化思考力:自创独门武功 3-3-3原则
  2. C#生成二维码(加源码)
  3. C++的可移植性和跨平台开发
  4. “全栈2019”Java第七十五章:内部类持有外部类对象
  5. Python常用功能函数
  6. Azure File挂载报错--System Error 1231
  7. python为何需要虚拟环境--Python虚拟环境的安装和配置-virtualenv
  8. fetch 请求列表ListView
  9. javax.jms.JMSException: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection timed out: connect
  10. ${__BeanShell(${SCRIPT})}