js打印

什么都不说,先上个图。


dayin.png

一. window.print()#

window.print();会弹出打印对话框,打印的是window.document.body.innerHTML中的内容。主流浏览器都支持这个方法

//这个样式是过滤不需要打印的内容的

<style media="print"> .Noprint{ display:none; } </style>

<body>
<div class="noprint" style="width:640px;height:20px;margin:100px auto 0 auto; font-size:12px;text-align:right;"> <input value="打印" type="button" onclick="window.print()" /> </div>
<div style="width:640px;height:624px;margin:20px auto;"> 这个是打印的内容,只要是body里面就行,不带noprint样式的。 </div>
</body>

二. document.execCommand(”print”)#

该方式跟window.print()差不多,但是不兼容火狐,其启动的是打印对话框,360极速模式,chrome的打印对话框自带预览功能,但是,360兼容模式,IE仅仅只弹出打印设置对话框,没有预览功能。

三. 调用windows底层打印#

yulan.png

shezhi.png

具体实现

<script type="text/javascript">
    function printsetup(){ 
        wb.ExecWB(8,1);// 打印页面设置
    }
    function printpreview(){ 
           wb.ExecWB(7,1);// 打印页面预览    
    }
    function printit() {
        wb.ExecWB(6,1);//打印
    }
</script>

<body>
<object id="wb" height=0 width=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" name="wb"> </object>
<div id="printButton" align=center style="margin-bottom: 10px" class="Noprint" > <input type="button" value="打印" name="button_print" style="cursor:pointer;" class="Btn uncheckBtn cornerRadius" onclick="printit()"/> <input onclick="printsetup();" type="button" style="cursor:pointer;" value="打印页面设置" name="button_setup" class="Btn uncheckBtn cornerRadius" /> <input type="button" value="打印预览" name="button_show" style="cursor:pointer;" class="Btn uncheckBtn cornerRadius" onclick="printpreview()"/> </div>
<div style="width:640px;height:624px;margin:20px auto;"> <strong>这个是打印的内容,只要是body里面就行,不带noprint样式的。</strong> </div>
</body>

四. jquery.PrintArea.js#

jquery.PrintArea.js下载地址:http://pan.baidu.com/s/1nu6eGzv
这个是一款jquery的插件,简单实用,支持局部打印功能。360极速模式,chrome的打印对话框自带预览功能,其他都是弹出打印对话框。效果跟以上类似

<script type="text/javascript" src="jquery-1.8.3.min.js"></script> <script type="text/javascript" src="jquery.PrintArea.js"></script>

function print(){ $("#print").printArea(); }
<div class="noprint" style="width:640px;height:20px;margin:100px auto 0 auto;font-size:12px;text-align:right;"> <input value="打印" type="button" onclick="print()" /> </div>
<div id="print" style="width:640px;height:624px;margin:20px auto;"> <strong>这个是打印的内容,只要是body里面就行,不带noprint样式的。</strong> </div>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,567评论 0 17
  • 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放...
    孤魂草阅读 915评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 城墙下是一棵棵枯光秃秃的树,叶子都被冻掉了。不知是什么缘故,一大早蜿蜒盘踞在群山上的城墙被大雾笼罩,眼前仿佛隔着一...
    橙蜂破浪阅读 319评论 0 0
  • 不知不觉中,《欢乐颂》已经迎来了大结局,在它播放到十几集的时候,网络上开始火了,各种点评它的帖子铺天盖地,我也看了...
    小勺一漪阅读 517评论 0 0