最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)瀏覽器打印功能的示例代碼

 更新時(shí)間:2020年07月15日 09:21:47   作者:小羽羽  
這篇文章主要介紹了js如何實(shí)現(xiàn)瀏覽器打印功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

最近接觸到一個(gè)新需求,實(shí)現(xiàn)打印機(jī)打印小票的功能。打的一桌子小票(慚愧),不過(guò)也基本滿足了業(yè)務(wù)上的需求,現(xiàn)在分享一下如何實(shí)現(xiàn)(好記性不如爛筆頭)

先上代碼

// 布局代碼
<div id="print">
    <div id="print_content"></div>
</div>
//js 部分代碼var f = document.getElementById('printf');
   if (f) {
    document.getElementById("print_content").removeChild(f);
   }
   var printhtml = `
   <div style="font-size:12px;margin-left: -6px;">
    <p style="margin-left:40px;">${this.ticket.title}</p>
    <p>--------------------------------------</p>
    <p>提貨點(diǎn):${this.ticket.pickUpAddress}</p>
    <p>商品名稱:${this.ticket.commodityName}</p>
    <p>下單時(shí)間:${this.ticket.paymentTime}</p>
    <p>提貨人:${this.ticket.receiver}</p>
    <p>聯(lián)系電話:${this.ticket.receiverPhone}</p>
    <p>提貨碼:${this.ticket.pickUpCode}</p>
    <p>提貨時(shí)間:${this.ticket.submissionTime}</p>
    <p style="color:#FFFFFF">.</p>
   </div>`
   if (!!window.ActiveXObject || "ActiveXObject" in window) { // 針對(duì)IE進(jìn)行適配
    var HKEY_Root, HKEY_Path, HKEY_Key;
    HKEY_Root = "HKEY_CURRENT_USER";
    HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
    //設(shè)置網(wǎng)頁(yè)打印的頁(yè)眉頁(yè)腳為空
    function PageSetup_Null() {
     var Wsh = new ActiveXObject("WScript.Shell");
     HKEY_Key = "header";
     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");
     HKEY_Key = "footer";
     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");
     HKEY_Key = "margin_left"
     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //鍵值設(shè)定--左邊邊界
   
     HKEY_Key = "margin_top"
     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //鍵值設(shè)定--上邊邊界
   
     HKEY_Key = "margin_right"
     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //鍵值設(shè)定--右邊邊界
   
     HKEY_Key = "margin_bottom"
     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //鍵值設(shè)定--下邊邊界
    }
    printhtml = `
    <div style="font-size:12px;font-weight: 800;height:150px;width:300px">
     <p style="margin-left:35px">${this.ticket.title}</p>
     <p>------------------------------------------------</p>
     <p>提貨點(diǎn):${this.ticket.pickUpAddress}</p>
     <p>商品名稱:${this.ticket.commodityName}</p>
     <p>下單時(shí)間:${this.ticket.paymentTime}</p>
     <p>提貨人:${this.ticket.receiver}</p>
     <p>聯(lián)系電話:${this.ticket.receiverPhone}</p>
     <p>提貨碼:${this.ticket.pickUpCode}</p>
     <p>提貨時(shí)間:${this.ticket.submissionTime}</p>
     <p style="color:#FFFFFF;font-weight: 100;">.</p>
    </div>`
   }
   var iframe = document.createElement('iframe');
   iframe.id = 'printf';
   iframe.style.width = '0';
   iframe.style.height = '0';
   iframe.style.border = "none";
   document.getElementById("print_content").appendChild(iframe);
   setTimeout(() => {
    iframe.contentDocument.write(printhtml);
    iframe.contentDocument.close();
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
   }, 100)

因?yàn)橐蟛荒馨汛蛴〉臄?shù)據(jù)顯示在頁(yè)面上,所以通過(guò)iframe的方式去實(shí)現(xiàn)。單純的截取字符串重新賦值body內(nèi)容能進(jìn)行打印卻把打印的內(nèi)容展現(xiàn)在頁(yè)面中了,所以不行。

打印針對(duì)IE的瀏覽器進(jìn)行了一定程度的調(diào)整,IE打印要做特定的處理,詳見上面判斷代碼。打印內(nèi)容通過(guò)模板字符串加內(nèi)聯(lián)樣式去實(shí)現(xiàn)。滿足了基本需求。

是否應(yīng)該也通過(guò)截取頁(yè)面字符串的方式去做可能比較浪費(fèi)性能些,為什么這么說(shuō)?因?yàn)闃邮皆诖蛴〉男∑鄙嫌幸欢ǔ潭鹊钠睿蘖藮|墻破了西墻,只能采取相對(duì)的方式取舍。如果這種寫法不滿足,可以采取截取字符串寫class嘗試。

以上就是js實(shí)現(xiàn)瀏覽器打印功能的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于js 瀏覽器打印的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

保亭| 闵行区| 海淀区| 白河县| 乐东| 繁昌县| 治县。| 蓝田县| 海丰县| 仁布县| 如皋市| 泰宁县| 枣阳市| 哈巴河县| 漳浦县| 绥棱县| 富裕县| 罗城| 海门市| 图木舒克市| 临猗县| 神木县| 从江县| 克山县| 铁力市| 临泽县| 海安县| 遂宁市| 金秀| 博兴县| 青州市| 航空| 汨罗市| 五峰| 大兴区| 云和县| 枝江市| 泊头市| 赣榆县| 延寿县| 宣城市|