您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關JS怎么現快遞單打印功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、js屬于一種解釋性腳本語言;2、在絕大多數瀏覽器的支持下,js可以在多種平臺下運行,擁有著跨平臺特性;3、js屬于一種弱類型腳本語言,對使用的數據類型未做出嚴格的要求,能夠進行類型轉換,簡單又容易上手;4、js語言安全性高,只能通過瀏覽器實現信息瀏覽或動態交互,從而有效地防止數據的丟失;5、基于對象的腳本語言,js不僅可以創建對象,也能使用現有的對象。
首先說一下js網頁打印的幾種方法:
1.window.print()
會彈出打印對話框
2.使用html 標簽引入Webbrowser控件
這種方式是其只兼容IE10以下的瀏覽器,其他瀏覽器不可使用
3.document.execCommand(”print”)
類似window.print()
**4.采用JQuery插件
5.用瀏覽器打印第三方插件如lodpod等**
由于我這邊不需要兼容ie10以下,而且個人不喜歡在react項目中引入jquery,同時嫌第三方插件太麻煩,最終選擇window.print(),
具體這幾種方法區別大家可以去網上查一下,這類博客很多。
廢話不多說,直接貼代碼
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>打印</title> <link rel="stylesheet" href="./print.css" rel="external nofollow" type="text/css"> <style type="text/css"> @import url("print.css") print; </style> <script language="javascript"> function remove_ie_header_and_footer() { let HKEY_Root, HKEY_Path, HKEY_Key; HKEY_Root = "HKEY_CURRENT_USER"; HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; try { let RegWsh = new ActiveXObject("WScript.Shell"); HKEY_Key = "header"; RegWsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, ""); HKEY_Key = "footer"; RegWsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, ""); } catch (e) {} } function printPage(printpage) { if (!!window.ActiveXObject || "ActiveXObject" in window) { //是否ie remove_ie_header_and_footer(); } let newstr = printpage.innerHTML; let oldstr = document.body.innerHTML; document.body.innerHTML =newstr; window.print(); document.body.innerHTML=oldstr; return false; } window.onload = function() { let bt=document.getElementById("bt"); let page=document.getElementById("printPage"); bt.onclick=function() { printPage(page); } } </script> </head> <body> <!--startPrint--> <div id="printPage" > <div id="emsType" class="left"> <span class="deviation">標準快遞</span> </div> <div id="barcode" class="right"> <span class="deviation">二維碼</span> </div> <div id="sender" class="left"> <span class="deviation">寄件:</span> <span>xxxx</span> </div> <div id="area" class="right"> <span class="deviation">xxx區</span> </div> <div id="recipient" class="left"> <span class="deviation">收件:</span> <span class="deviation">xxxx人xxxx手機xxxxxxx</span> <span class="deviation">xxxxxxxx小區</span> </div> <div id="pay" class="left"> <span class="deviation">付款方式:</span> <span></span> <br/> <span class="deviation">計費重量(KG):</span> <span></span> <br/> <span class="deviation">保價金額(元):</span> <span></span> <br/> </div> <div id="delivers" class="right"> <span class="deviation">收件人\代收人:</span> <span></span> <br/> <span class="deviation">簽收時間:</span> <span></span> <span>年</span> <span></span> <span>月</span> <span></span> <span>日</span> <span></span> <span>時</span> <br/> <span id="note">快件送達收貨人地址,經收件人或收件人允許的代收人簽字,視為送達</span> </div> <div id="order" class="left"> <span class="deviation">訂單號:</span> <span></span> <span class="deviation">件數:</span> <span></span> <span class="deviation">重量(KG):</span> <span></span> <span></span> </div> <div id="divide" class="left"> </div> <div id="number" class="left right"> <span class="deviation">條碼</span> </div> <div id="send" class="left right"> <span class="deviation">寄件:</span> <span></span> </div> <div id="pickup" class="right"> <span class="deviation">收件:</span> <span></span> </div> <div id="remark" class="left"> <span class="deviation">備注:</span> <span></span> </div> <div id="net" class="left"> <span class="deviation">www.ems.cn 客服電話:11183</span> </div> <div id="QRCode" class="right"> <span class="deviation">二維碼</span> </div> </div> <!--endPrint--> <input name="print" class=" no-print" type="button" id="bt" value="點擊打印" /> </body> </html>
通過css3 print控制打印樣式,不需要打印的可以通過添加 class為noPrint來隱藏
@media print{ body, h2, h3, h4, h5, h6, h7, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td { margin:0; padding:0; } body, button, input, select, textarea { font:12px/1.5 arial,"黑體", \5b8b\4f53; } h2, h3, h4, h5, h6, h7{ font-size:100%; } address, cite, dfn, em, var { font-style:normal; } code, kbd, pre, samp { font-family:couriernew, courier, monospace; } small{ font-size:12px; } ul, ol { list-style:none; } a { text-decoration:none; } a:hover { text-decoration:underline; } sup { vertical-align:text-top; } sub{ vertical-align:text-bottom; } legend { color:#000; } fieldset, img { border:0; } button, input, select, textarea { font-size:100%; } table { border-collapse:collapse; border-spacing:0; } .no-print{ display:none; } @page{ size: 100mm 150mm; margin: 1.75mm; } #printPage { width: 96mm; height: 146mm; position: relative; border: 1px dotted #000000; box-sizing: border-box; } #printPage span{ margin-left: 2mm; } .left { border-left: 1px dotted #000000; border-right: 1px dotted #000000; border-bottom: 1px dotted #000000; left: 0; } .right { border-right: 1px dotted #000000; border-bottom: 1px dotted #000000; /*right: 0;*/ } .deviation{ margin-left: 2mm; } #emsType{ width: 40mm; height: 14mm; position: absolute; top: 0; padding-top: 6mm; font-size: 20px; border-top: 1px dotted #000000; } #barcode{ width: 56mm; height: 20mm; position: absolute; top: 0; left: 40mm; border-top: 1px dotted #000000; } #sender{ width: 46mm; height: 15mm; font-size: 12px; position: absolute; top: 20mm; } #area{ width: 50mm; height: 15mm; position: absolute; top: 20mm; left: 46mm; } #recipient{ width: 96mm; height: 17mm; position: absolute; left: 0; top: 35mm; font-size: 12px; } #pay{ width: 40mm; height: 14mm; position: absolute; top: 52mm; font-size: 12px; } #delivers{ width: 56mm; height: 14mm; position: absolute; top: 52mm; left: 40mm; font-size: 12px; } #order{ width: 96mm; height: 20mm; position: absolute; left: 0; top: 66mm; font-size: 12px; } #divide{ width: 96mm; height: 4mm; position: absolute; left: 0; top: 86mm; } #number{ width: 96mm; height: 15mm; position: absolute; left: 0; top: 90mm; font-size: 12px; } #send{ width: 40mm; height: 17mm; position: absolute; top: 105mm; font-size: 12px; } #pickup{ width: 56mm; height: 17mm; position: absolute; top: 105mm; left: 40mm; font-size: 12px; } #remark{ width: 68mm; height: 17mm; position: absolute; top: 122mm; font-size: 12px; } #net{ width: 68mm; height: 7mm; position: absolute; top: 139mm; font-size: 12px; } #QRCode{ width: 28mm; height: 24mm; position: absolute; top: 122mm; left: 68mm; } #note{ font-size: 6px; } }
一開始在ff和chrome打印出來的會有細微差別,這是由于瀏覽器自帶樣式的影響,清除瀏覽器樣式后基本沒區別了還有就是chrome需要將瀏覽器設置里自定義字體中最小字體調到最低,不然會影響打印效果 ff這需要在打印里設置頁頭頁腳為空白,不然打印的帶頁頭頁腳。
此外chrome打印出來的比ff清楚,這點原因我還沒搞清楚。
貼兩張打印預覽,上面是chrome,下面是ff
感謝各位的閱讀!關于“JS怎么現快遞單打印功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。