什么都不說(shuō),先上個(gè)圖。
一. window.print()#
window.print();會(huì)彈出打印對(duì)話(huà)框,打印的是window.document.body.innerHTML中的內(nèi)容。主流瀏覽器都支持這個(gè)方法
//這個(gè)樣式是過(guò)濾不需要打印的內(nèi)容的
<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;"> 這個(gè)是打印的內(nèi)容,只要是body里面就行,不帶noprint樣式的。 </div>
</body>
二. document.execCommand(”print”)#
該方式跟window.print()差不多,但是不兼容火狐,其啟動(dòng)的是打印對(duì)話(huà)框,360極速模式,chrome的打印對(duì)話(huà)框自帶預(yù)覽功能,但是,360兼容模式,IE僅僅只彈出打印設(shè)置對(duì)話(huà)框,沒(méi)有預(yù)覽功能。
三. 調(diào)用windows底層打印#
具體實(shí)現(xiàn)
<script type="text/javascript">
function printsetup(){
wb.ExecWB(8,1);// 打印頁(yè)面設(shè)置
}
function printpreview(){
wb.ExecWB(7,1);// 打印頁(yè)面預(yù)覽
}
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="打印頁(yè)面設(shè)置" name="button_setup" class="Btn uncheckBtn cornerRadius" /> <input type="button" value="打印預(yù)覽" name="button_show" style="cursor:pointer;" class="Btn uncheckBtn cornerRadius" onclick="printpreview()"/> </div>
<div style="width:640px;height:624px;margin:20px auto;"> <strong>這個(gè)是打印的內(nèi)容,只要是body里面就行,不帶noprint樣式的。</strong> </div>
</body>
四. jquery.PrintArea.js#
jquery.PrintArea.js下載地址:http://pan.baidu.com/s/1nu6eGzv
這個(gè)是一款jquery的插件,簡(jiǎn)單實(shí)用,支持局部打印功能。360極速模式,chrome的打印對(duì)話(huà)框自帶預(yù)覽功能,其他都是彈出打印對(duì)話(huà)框。效果跟以上類(lèi)似
<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>這個(gè)是打印的內(nèi)容,只要是body里面就行,不帶noprint樣式的。</strong> </div>