js打印

作者: 天空中的牛 | 来源:发表于2017-05-16 13:55 被阅读0次

什么都不说,先上个图。


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>

相关文章

网友评论

      本文标题:js打印

      本文链接:https://www.haomeiwen.com/subject/lozotxtx.html